$(document).ready(function(){
	$(document).pngFix();
	
		
	//optimize content box for window height
	function optimizeContentBox () {
		
		
		if (window.innerHeight) { 
			$iWindowHeight = window.innerHeight;
		}
		
		if (window.document.documentElement.clientHeight) {
			$iWindowHeight = window.document.documentElement.clientHeight;
		}
		
		if (window.innerHeight || window.document.documentElement.clientHeight) {
		
			$iProductsContentHeight = ($iWindowHeight - 210);
			$iBrandsContentHeight = ($iWindowHeight - 360);	
			$iAboutContentHeight = ($iWindowHeight - 555);	
			$iImpressumContentHeight = ($iWindowHeight - 500);
			$iAgbContentHeight = ($iWindowHeight - 500);
			//$iContactContentHeight = ($iWindowHeight - 515);
			$iAdressContentHeight = ($iWindowHeight - 230);
			
			if ($iProductsContentHeight < 200) {
				$iProductsContentHeight = 200;
			}
			
			if ($iBrandsContentHeight < 280) {
				$iBrandsContentHeight = 280;
			}
			
			if ($iAboutContentHeight < 280) {
				$iAboutContentHeight = 280;
			}
			
			if ($iImpressumContentHeight < 280) {
				$iImpressumContentHeight = 280;
			}
			
			if ($iAgbContentHeight < 280) {
				$iAgbContentHeight = 280;
			}
			
			//if ($iContactContentHeight < 700) {
			//	$iContactContentHeight = 700;
			//}
			
			$('.products #Wrapper .content .content-wrapper .content-inner .box').height($iProductsContentHeight);
			$('#Wrapper .content .content-wrapper .content-inner .abox').height($iAboutContentHeight);
			$('.brands #Wrapper .content .content-wrapper .content-inner .box').height($iBrandsContentHeight);
			//$('#Wrapper .content .content-wrapper .impressum-box .inner-box').height($iImpressumContentHeight);
			$('#Wrapper .content .content-wrapper .agb-box .inner-box').height($iAgbContentHeight);
			$('#Wrapper .content .content-wrapper .contactform-inner .box').height($iContactContentHeight);
			$('#Wrapper .content .content-wrapper #Map').height($iAdressContentHeight);
		
		}
	
	}
	
	
	//fire the dynamic height for content box
	//optimizeContentBox();
	
	
	$('#tBrands').click(function() {
        if ( $(this).hasClass('active') )
        {
            $('#oBrands').slideUp(200, function() {
                $('#tBrands').removeClass('active');
            });
        }
        else
        {
            $('#oBrands').slideDown(350, function() {
                $('#tBrands').addClass('active');
            });
        }
        return false;
    });
	
	$(".has-sub-nav").hover(function(){
		$('.has-sub-nav ul:animated').stop(true,true);
		$('ul.main li ul').hide();
		$('ul.main li a').removeClass("slideview");
		$(this).css({'z-index':'9999'});
		$(this).find('ul:first').css({'z-index':'9999'});
		$(this).find('ul:first').slideDown(150);
		$(this).find('a:first').addClass('slideview');
	},function(){
		var _self = this;
		$(this).find('ul:first').slideUp(250, function () {
		    $(_self).css({'z-index':'0'});
		    $(_self).find('ul:first').css({'z-index':'0'});
			$(_self).find('a:first').removeClass('slideview');
		});
	});
	
	/*
*/
	$(".tContactFormDropdown").click(function () {
		$(this).find('ul:first').slideDown(200);
		$(this).addClass('open');
		
	});
	
	
	$(".tGalleryOverlay").hover(function(){
		$(this).find('span:first').slideDown(150);
		},function(){
		$(this).find('span:first').slideUp(150);
	 });
	
	$(".tHomeGalleryOverlay").hover(function(){
		$(this).find('span:first').fadeIn(150);
		},function(){
		$(this).find('span:first').fadeOut(300);
	 });
	
	$('#slct-title').selectbox({
		onChangeCallback: slctFunction
	});
    
	$('#slct-region').selectbox({
		onChangeCallback: slctFunction
	});
	
	function slctFunction(args)
	{
		$('span.change').append(args.selectedVal);
	}
	
	$(".image-col ul.gallery li a").colorbox();
	
	/// fire scrollPane
	setTimeout(function(){
		var iBoxHeight = $('.content .box .agb-box').height();
		var iInnerBoxHeight = $('.content .inner-box .agb-inner-box').height();
		if (iInnerBoxHeight > iBoxHeight) {
			$('.content .content-wrapper .content-inner').css("width","317px").css("paddingRight","13px");
			$('.content .content-wrapper .content-inner .box').css("width","317px");
			$('.content .content-wrapper .agb-inner-box').css("width","517px").css("paddingRight","13px");
			$('.content .content-wrapper .agb-inner-box .agb-box').css("width","600px");
			$('.content .box').jScrollPane({scrollbarWidth:7, scrollbarMargin:10 });
			$('.content .abox').jScrollPane({scrollbarWidth:7, scrollbarMargin:10 });
			$('.impressum-box .inner-box').jScrollPane({scrollbarWidth:7, scrollbarMargin:10 });
			$('.contactform-inner .box').jScrollPane({scrollbarWidth:7, scrollbarMargin:10 });
		}
	}, 1000);
	
	//initiate background change for watches
	$('#oBrands.has-watches ul li img').each(function(){
		
		var sThisSrc = $(this).attr("src");
		var sThisRel = $(this).attr("rel");
		var iPosOfLastSlash = strrpos(sThisSrc, "/");
		var sBeginningOfNewPath = sThisSrc.substring( 0, iPosOfLastSlash );
		var sEndOfNewPath = sThisSrc.substring( iPosOfLastSlash, sThisSrc.length )
			sEndOfNewPath = sEndOfNewPath.replace(".png", ".jpg");
		if ( sThisRel == "zoom" )
		{
		    var sNewPath = sBeginningOfNewPath + "/zoom" + sEndOfNewPath;
		}
		else
		{
		    var sNewPath = sBeginningOfNewPath + "/_large" + sEndOfNewPath;
		}
		
		$(this).wrap('<a href="' + sNewPath + '" class="bg-changer"></a>');
	});
	
	// change bg image on click
	$('#oBrands.has-watches ul li a.bg-changer').click(function(){
		
		var sNewBGPic = $(this).attr("href");
		$('html body').css("background", "url(" + sNewBGPic + ") no-repeat fixed left top #000000");
		$('#ImageFader').remove();
		
		return false;
	});
	
	//initiate background change for about-us
	$('#galery ul.load-background li img').each(function(){
		
		var sThisSrc = $(this).attr("src");
		var iPosOfLastSlash = strrpos(sThisSrc, "/");
		var sBeginningOfNewPath = sThisSrc.substring( 0, iPosOfLastSlash );
		var sEndOfNewPath = sThisSrc.substring( iPosOfLastSlash, sThisSrc.length )
			sEndOfNewPath = sEndOfNewPath.replace(".png", ".jpg");
		var sNewPath = sBeginningOfNewPath + "/_large" + sEndOfNewPath;
		$(this).wrap('<a href="' + sNewPath + '" class="bg-changer"></a>');
	});
	
	// change bg image on click
	$('#galery ul.load-background li a').click(function(){
		
		var sNewBGPic = $(this).attr("href");
		var sRelAttr = $(this).attr("rel");
		if ( sRelAttr == "center" )
		{
		    $('html body').css("background", "url(" + sNewBGPic + ") no-repeat fixed left center #000000");
		}
		else
		{
		    $('html body').css("background", "url(" + sNewBGPic + ") no-repeat fixed left top #000000");
		}
		
		return false;
	});
	
	
	
	// handle brand more box
	$('.brand-buttons .btn-more').click(function(){
	    toggleBrandMoreBox();
	    return false;
	});
	$('#LearnMore a.close').click(function(){
	    toggleBrandMoreBox();
        return false;
    });
	
	function toggleBrandMoreBox()
	{
	    $('#LearnMore').animate({width: 'toggle'}, function(){
	        $(this).toggleClass('open');
	        $('.brand-buttons .btn-more').toggleClass('active');
	        var font = {src: '/src/flv/cochin.swf'};
	        sIFR.replace(font, {selector: 'h2', wmode: 'transparent' ,css: {'.sIFR-root': { 'color': '#FFFFFF', 'text-transform': 'uppercase' } }});
	    });
	    
	    if ( $('#LearnMore').hasClass('open') )
        {
	        $('#oBrands').slideDown(350, function() {
	            $('#tBrands').addClass('active');
                $('#ImageFader').fadeIn('slow');
            });
        }
        else
        {
            $('#oBrands').slideUp(200, function() {
                $('#tBrands').removeClass('active');
                $('#ImageFader img').fadeOut('slow', function(){
                    $('#ImageFader').hide();
                });
            });
        }
        return false;
	}
	

	StartImageFader.init();
});


var StartImageFader = {
    
    iFadeTimeout : 5000,
        
    iFadeSpeed : 3000,
        
    oFadeImage : null,
    
    iCurrentIndex : 0,

    init : function() 
    {
        StartImageFader.oFadeImage = $('#StartFadeImage');
        
        window.setTimeout(function(){ StartImageFader.fade(); }, StartImageFader.iFadeTimeout);
    },
    
    fade : function() 
    {
        if (StartImageFader.iCurrentIndex == aWatches.length) {
            StartImageFader.iCurrentIndex = 0; 
        } else {
            StartImageFader.iCurrentIndex++;
        }

        StartImageFader.oFadeImage.fadeOut(StartImageFader.iFadeSpeed, function(){
            StartImageFader.oFadeImage.attr('src', aWatches[StartImageFader.iCurrentIndex]);

            StartImageFader.oFadeImage.fadeIn(StartImageFader.iFadeSpeed, function(){
                window.setTimeout(function(){ StartImageFader.fade(); }, StartImageFader.iFadeTimeout);
            });
        });
    }
};


var currentFormElem = null;
var showHideValue = function(obj,val)
{
    if (obj != currentFormElem)
    {
        currentFormElem = obj;
        if (obj.value == val)
            obj.value = '';
            
        obj.onblur = function()
        {
            showHideValue(obj,val);
        }
    }
    else
    {
        if (obj.value == '')
            obj.value = val;
        
        obj.onblur = null;
        
        currentFormElem = null;
    }
};

// function strrpos
function strrpos (haystack, needle, offset) {
    // Finds position of last occurrence of a string within another string  
    // 
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/strrpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   input by: saulius
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strrpos('Kevin van Zonneveld', 'e');    // *     returns 1: 16
    // *     example 2: strrpos('somepage.com', '.', false);
    // *     returns 2: 8
    // *     example 3: strrpos('baa', 'a', 3);
    // *     returns 3: false    // *     example 4: strrpos('baa', 'a', 2);
    // *     returns 4: 2
    var i = -1;
    if (offset) {
        i = (haystack+'').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,        // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
        if (i !== -1) {
            i += offset;
        }
    }    else {
        i = (haystack+'').lastIndexOf(needle);
    }
    return i >= 0 ? i : false;
}
