//---------------------------------//
//JQUERY
//---------------------------------//

Cufon.replace('#header .lang', 			{ fontFamily: 'Century Gothic' });
Cufon.replace('#slider-home h1', 		{ fontFamily: 'Calibri' });
Cufon.replace('#slider-home h1 span', 	{ fontFamily: 'Calibri' });

Cufon.replace('#content h1', 				{ fontFamily: 'Century Gothic' });
Cufon.replace('#howitworks h1', 			{ fontFamily: 'Calibri' });
Cufon.replace('#howitworks h2', 			{ fontFamily: 'Century Gothic' });
Cufon.replace('#howitworks #lightbox h2', 	{ fontFamily: 'Calibri' });

Cufon.replace('#wrapper h1', 		{ fontFamily: 'Calibri' });

Cufon.replace('.box.download h3',	 	{ fontFamily: 'Calibri' });
Cufon.replace('.box.download a', 		{ fontFamily: 'Calibri' });
Cufon.replace('.box.download-small a', 	{ fontFamily: 'Calibri' });

Cufon.replace('#products-default a h2', { fontFamily: 'Calibri' });
Cufon.replace('#support h1', 			{ fontFamily: 'Calibri' });

function popUp(theURL,winName,features) { 
	window.open(theURL,winName,features);
}

function openUrl(theURL) { 
	window.location = theURL;
	return false;
}

$(document).ready(function() {	
		
	//---------------------------------
	//START - MAINMENU
	//---------------------------------	
		var enable_timeout = false;
		$("#menu .topnav li").each(function(){
			
			var subNavExist = $(this).find("ul.subnav").length;
			if(subNavExist == 1){ 
				//$(this).append("<span></span>"); 
				$(this).find("a").not("ul.subnav li a").css("padding-right","20px");
				$(this).find("a").not("ul.subnav li a").before("<span></span>");
			}
		});	
		
		
		$.disableActive = function() {
			$("#menu .topnav").find("li#active").attr("id", "active2");
		};
		
		$.enableActive = function() {
			$("#menu .topnav").find("li#active2").attr("id", "active");
		};	
		
		
		$("ul.topnav li").not("ul li ul li").mouseover(function() { 
			
			$dropdownMenu = $(this);
			$(this).addClass('active');	
			$.disableActive();		
			$("ul.subnav",this).css("display", "block"); 
			
			if(enable_timeout){
				clearTimeout(enable_timeout);
			}		
			
			$(this).hover(function() {
			}, function(){	
				$dropdownMenu.removeClass('active');
				
				if(enable_timeout){
					clearTimeout(enable_timeout);
				}			
				
				enable_timeout = setTimeout('$.enableActive();',1500);
				
				$(this).find("ul.subnav").css("display", "none"); 
			});
	 
			}).hover(function() { 
				$(this).addClass("subhover"); 
			}, function(){	
				$(this).removeClass("subhover");
				
		});
	//---------------------------------
	//END - MAINMENU
	//---------------------------------		
	
	
	
	
	
	//*********************************
	//START - GLOBAL PAGE SETTINGS
	//*********************************	
		
		//start lightbox
		$(".screenshots a[data-rel^='screenshots']").prettyPhoto({animationSpeed:'slow',theme:'facebook', social_tools: false, overlay_gallery: false});
		$("a[data-rel^='youtube']").prettyPhoto({animationSpeed:'slow',theme:'facebook', social_tools: false, overlay_gallery: false, default_width:600, default_height:340});

		
	    //start pngFix
		$('div').pngFix();
			  
	    //start alt-title
	    $('.social').attr('title', function() {
	    	return this.alt;
	    });   		         	
					
		
		//check if submenu exist, if exist do nothing
		if (!$('#content-default div').hasClass('subnav')) {
			$('#content-default .left').css("width","750px");
		}
				
	    
		//START SEARCH HEADER
		var searchField = "#header input[name=search]";
		var searchValue = $(searchField).val();
	    $(searchField).focus(function(){
			if ($(this).val() == searchValue) {
				$(this).val("");
				$(this).css("color", "#000000");	
			}
		});
		$(searchField).blur(function(){
			if (!$(this).val()) {
				$(this).val(searchValue);
				$(this).css("color", "#b3b3b3");
			}
		});				
		//END SEARCH HEADER
     
		
		//START DROPDOWN PRODUCT HEADER		
		
			/*  function selectproduct movein animation */
			$.sp_moveIn = function() {
				$("#header .selectproduct_off").removeClass('selectproduct_off').addClass('selectproduct_on');
				$("#container #db_products").slideDown(100);
			};

			/*  function selectproduct moveout animation */
			$.sp_moveOut = function() {
				$("#header .selectproduct_on").removeClass('selectproduct_on').addClass('selectproduct_off');
				$("#container #db_products").slideUp(100);
			};	
		
		var sp_timeout = false;
		$("#header .selectproduct_off").click(
			function() {			
				if ($(this).hasClass('selectproduct_off')) { 
					$.sp_moveIn();		
				}
				else {
					$.sp_moveOut();	
				}	
			}		
		);
		
		//mouse-over
		$("#header .selectproduct_on, #header .selectproduct_off, #container #db_products").mouseover(
			function() {
				if(sp_timeout){
					clearTimeout(sp_timeout);
				}
			}
		);
		//mouse-out
		$("#header .selectproduct_on, #header .selectproduct_off, #container #db_products").mouseout(
			function() {
				sp_timeout = setTimeout('$.sp_moveOut();',500);
			}
		);	
		//END DROPDOWN PRODUCT HEADER		
		
		
		//START NEWSLETTER BOX
		var emailField = ".box input[name=email]";
		var emailValue = $(emailField).val();
	    $(emailField).focus(function(){
			if ($(this).val() == emailValue) {
				$(this).val("");
				$(this).css("color", "#000000");	
			}
		});
		$(emailField).blur(function(){
			if (!$(this).val()) {
				$(this).val(emailValue);
				$(this).css("color", "#898989");
			}
		});				
		//END NEWSLETTER BOX
	
		
		//START DISCOUNT BOX
		$(".discount #product .price span").each(
			function() {
				//start price value
				var value = $(this).html().split(",");
				$(this).html('<div class="num">'+value[0]+'</div><img class="dot" src="template/images/labels/img-6455-dot-red.gif" border="0" alt="" width="9" height="9"/><div class="dec">'+value[1]+'</div>');
				//end price value
			}	
		);
		Cufon.replace('.discount #product .price .num', { fontFamily: 'Myriad Pro' });
		Cufon.replace('.discount #product .price .dec', { fontFamily: 'Myriad Pro' });	
		
		$(".discount #product .price2 span").each(
				function() {
					//start price value
					var value = $(this).html().split(",");
					$(this).html('<div class="num">'+value[0]+'</div><div class="percent">%</div>');
					//end price value
				}	
					
		);
		Cufon.replace('.discount #product .price2 .num', { fontFamily: 'Myriad Pro' });
		Cufon.replace('.discount #product .price2 .percent', { fontFamily: 'Myriad Pro' });	
		
		//END DISCOUNT BOX
		

	//*********************************
	//END - GLOBAL PAGE SETTINGS
	//*********************************
    
		
		
		
    
	//*********************************
	//START PAGE HOME
	//*********************************	    
    
	    //start slideshow		
	    slideShow();		
		
		//start product-slider
	    $("#productslider-home #slider").codaSlider();		

	    //start howitworks lightbox
	    $.howitworks = {version: '1.0'};
		$.fn.howitworks = function() {
	    
		    $hitw_lightbox 		= $("#howitworks #lightbox");
		    $hitw_lightbox_mask = $hitw_lightbox.find("#mask");
		    
		    $hitw_lightbox_h2 = $hitw_lightbox.find("h2");
		    
		    //create buttons
		    $hitw_lightbox_h2.append('<img src="template/themes/top-fotoalbum/images/icon-previous-button.gif" width="21" height="21" alt="" class="prev"/><img src="template/themes/top-fotoalbum/images/icon-next-button.gif" width="21" height="21" alt="" class="next"/><img src="template/themes/top-fotoalbum/images/icon-close-button.gif" width="18" height="18" alt="" class="close"/>');
		    
		    if($hitw_lightbox.length){
		    	
		    	$.howitworks.resize_overlay();
		    	$.howitworks.center_content();
			 
			    //resize lightbox
			    $(window).resize(function() {
			    	$.howitworks.resize_overlay();
			    	$.howitworks.center_content();
			    });
			    
			    //scroll lightbox
			    $(window).scroll(function() {
			    	$.howitworks.center_content();
			    });
			    		    
			    //start lightbox
			    $(this).click(function(){
			    	$hitw_lightbox.css('top','0px');
			    	
			    	var step = $(this).attr("href");
			    	$.howitworks.open_step(step);
			    	
					return false;
				});	
			    
			    //previous lightbox
			    $hitw_lightbox_h2.find('.prev').click(function(){
			    	var step = $.howitworks.active_step();
			    	step = '#step'+(parseInt(step)-1);
			    	$.howitworks.open_step(step);
					return false;
				});				    
			    
			    //next lightbox
			    $hitw_lightbox_h2.find('.next').click(function(){
			    	var step = $.howitworks.active_step();
			    	step = '#step'+(parseInt(step)+1);
			    	$.howitworks.open_step(step);
					return false;
				});		
			    
			    
			    //close lightbox
			    $hitw_lightbox.find('.overlay, .close').click(function(){
			    	$hitw_lightbox.css('top','-99999px');
					return false;
				});		    

		    }
    
		    return false;
	    };
	    
	    //scroll function
	    $.howitworks.get_scroll = function() {	
			if (self.pageYOffset) {
				return {scrollTop:self.pageYOffset,scrollLeft:self.pageXOffset};
			} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
				return {scrollTop:document.documentElement.scrollTop,scrollLeft:document.documentElement.scrollLeft};
			} else if (document.body) {// all other Explorers
				return {scrollTop:document.body.scrollTop,scrollLeft:document.body.scrollLeft};
			};
		};	    
	    
		 //resize overlay function
	    $.howitworks.resize_overlay = function() {	
	    	$position_container = $("#container").offset();
	    	
		    $hitw_lightbox.css('left',(0-($position_container.left))+'px');
		    $hitw_lightbox.height($(document).height()).width($(document).width());
		    
	    };	
	    
	    //center content function
	    $.howitworks.center_content = function() {	
		    
		    windowHeight 	= $(window).height();
		    windowWidth 	= $(window).width();
		    
			scroll_pos 		= $.howitworks.get_scroll();
			lightboxHeight 	= $hitw_lightbox_mask.height();
			lightboxWidth 	= $hitw_lightbox_mask.width();

			projectedTop = (windowHeight/2) + scroll_pos['scrollTop'] - (lightboxHeight/2);
			if(projectedTop < 0) projectedTop = 0;
			
			if(lightboxHeight > windowHeight)
				return;
			
			$hitw_lightbox_mask
			.stop()
			.animate({
				'top': projectedTop,
				'left': (windowWidth/2) + scroll_pos['scrollLeft'] - (lightboxWidth/2)
			}, "slow" );	
			
			/*
			$hitw_lightbox_mask.css({
				'top': projectedTop,
				'left': (windowWidth/2) + scroll_pos['scrollLeft'] - (lightboxWidth/2)
			});
			*/
			
	    }
	    
	    //active step function
		$.howitworks.active_step = function() {	
			
			var step = false;
			$hitw_lightbox.find("div.box").each(function() { 
				if($(this).css('top') == '0px'){
					step = $(this).attr("data-rel");
				}
			});
	
			return step;
			
		}	
		
	    //show or hide step function
		$.howitworks.get_steps = function() {	
			
			var step = $.howitworks.active_step();
			var prev = parseInt(step)-1;
			var next = parseInt(step)+1;
			
			var prevbutton = $hitw_lightbox_h2.find('.prev');
			var nextbutton = $hitw_lightbox_h2.find('.next'); 
			
			//previous: show or hide
			if($hitw_lightbox.find("div.box[data-rel="+prev+"]").length){ prevbutton.show(); }
			else { prevbutton.hide(); } 
			
			//next: show or hide
			if($hitw_lightbox.find("div.box[data-rel="+next+"]").length){ nextbutton.show(); }
			else { nextbutton.hide(); } 			
			
			return false;
			
		}		
	    
	    //open step function
		$.howitworks.open_step = function(step) {	
			
			$hitw_lightbox.find("div.box").css('top','-99999px');
			
			var step = step.split('#step')[1];
			$hitw_lightbox.find("div[data-rel="+step+"]").css('top','0px');
			
			//hide or show steps
			$.howitworks.get_steps();			
			
			return false;
			
		}	    
	    
		$("a[data-rel^='howitworks']").howitworks();
		
	//*********************************
	//END PAGE HOME
	//*********************************	 
    

		
		
	
	//*********************************
	//START PAGE PRODUCTS
	//*********************************		
	
		//start products item/image marge
		var i = 0;
		$("#products-default .item, #products-default .clear").each(
			function() {			
				var className 	= $(this).attr('class'); 
				var imgObj	 	= $(this).find('img').not("img#hd");
				var hdObj	 	= $(this).find('img#hd');
				
				var spanObj	 	= $(this).find('span').not(".price span");
				var imgWidth	= imgObj.width();
				
				//image
				imgObj.addClass('product');
				spanObj.addClass('link');
				
				imgObj.css("margin-left","-"+(imgWidth/2)+"px");
				hdObj.css("right",((253-imgWidth)/2));
				
				//marge
				if(className == 'clear'){ if(i%2 == 0){ i=i+1; } }
				if(i%2 == 0){
					$(this).css("margin-right","13px");
				}
				i++;
				
			}		
		);
		//end products item marge
	
		//start price value
		$("#products-default .price span").each(
			function() {
					
				var value = $(this).html().split(",");
				$(this).html('<div class="num">'+value[0]+'</div><img class="dot" src="template/images/labels/img-5846-dot-red.gif" border="0" alt="" width="7" height="7"/><div class="dec">'+value[1]+'</div>');

			}	
					
		);
		Cufon.replace('#products-default .price .num', { fontFamily: 'Myriad Pro' });
		Cufon.replace('#products-default .price .dec', { fontFamily: 'Myriad Pro' });
		//end price value		
		
		
	//*********************************		
	//END PAGE PRODUCTS
	//*********************************		
	
		
	//*********************************		
	//START PAGE DOWNLOAD
	//*********************************	

		$("#page-download a.button").each(
			function() {
				
				var rel= $(this).attr("data-rel");
				
				$(this).attr("href",'http://'+window.location.hostname+"/nl/download.ajax.php?ajax=true&width=480&height=190&link="+rel);	
				$(this).prettyPhoto({animationSpeed:'slow',theme:'facebook',default_width:'480',default_height:'190', social_tools: false, overlay_gallery: false});	
			}	
		);		
		
	//*********************************		
	//END PAGE DOWNLOAD
	//*********************************		
		
		
	//*********************************		
	//START PAGE TIPUWVRIENDEN
	//*********************************			
		
		$("#page-tipuwvrienden a.tipbutton").each(
			function() {
				
				var rel= $(this).attr("data-rel");
				
				$(this).attr("href",'http://'+window.location.hostname+"/nl/tipuwvrienden.ajax.php?ajax=true&width=640&height=480&link="+rel);	
				$(this).prettyPhoto({animationSpeed:'slow',theme:'facebook',default_width:'640',default_height:'480', social_tools: false, overlay_gallery: false });
				
			}	
		);		
							
	//*********************************		
	//END PAGE TIPUWVRIENDEN
	//*********************************				
		
		
});   


//*********************************		
//TIPUWVRIENDEN
//*********************************	
function checkEmailTip(value){
	
	var result = false;
	
	$.ajax({
		type: "POST",
		url: "core/components/tipuwvrienden/checkemail.ajax.php",
		dataType: "json",
		async: false,  
		data: {
			email : value
		},		
		success : function(data){
			result = data.check; 
		}
	});
	
	return result;
}


function validateTip(id){
	
	var yourname 	= $(id).find("#yourname").val();
	var youremail 	= $(id).find("#youremail").val();
	var email1 		= $(id).find("#email1").val();
	var email2 		= $(id).find("#email2").val();
	var email3 		= $(id).find("#email3").val();
	
	var validate = true;
	
	$('#checkmail_1').hide();
	$('#checkmail_2').hide();
	$('#checkmail_3').hide();
	
	if(yourname == ''){
	    alert("U heeft geen naam opgegeven!");
	    validate = false;		
	}
	else {
	
		if (!/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(youremail)) {
		    alert("Uw e-mailadres is onjuist!");
		    validate = false;
		}
		else {
	
			if (!/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email1)) {
			    alert("Het opgegeven eerste e-mailadres is onjuist!");
			    validate = false;
			}
			else {
			
				if (!/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email2)) {
				    alert("Het opgegeven tweede e-mailadres is onjuist!");
				    validate = false;
				}
				else {
				
					if (!/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email3)) {
					    alert("Het opgegeven derde e-mailadres is onjuist!");
					    validate = false;
					}
					else {
						
						if(youremail == email1){  
							validate = false;
							alert("Het eerste e-mailadres komt overeen met uw e-mailadres.\nDit wordt niet toegestaan!"); 
						}
						else {
							if(youremail == email2){  
								validate = false; 
								alert("Het tweede e-mailadres komt overeen met uw e-mailadres.\nDit wordt niet toegestaan!"); 
							}
							else {
								if(youremail == email3){  
									validate = false; 
									alert("Het derde e-mailadres komt overeen met uw e-mailadres.\nDit wordt niet toegestaan!"); 
								}
								else {
									if(email1 == email2){  
										validate = false; 
										alert("Het eerste e-mailadres komt overeen met het tweede e-mailadres.\nDit wordt niet toegestaan!"); 
									}
									else {
										if(email1 == email3){  
											validate = false; 
											alert("Het eerste e-mailadres komt overeen met het derde e-mailadres.\nDit wordt niet toegestaan!"); 
										}
										else {
											if(email2 == email3){  
												validate = false; 
												alert("Het tweede e-mailadres komt overeen met het derde e-mailadres.\nDit wordt niet toegestaan!"); 
											}
											else {
											
												//email1 already exist
												if(checkEmailTip(email1)){
													$('#checkmail_1').show();
													validate = false;
												}													
												
												//email2 already exist
												if(checkEmailTip(email2)){
													$('#checkmail_2').show();
													validate = false;
												}												
												
												//email1 already exist
												if(checkEmailTip(email3)){
													$('#checkmail_3').show();
													validate = false;
												}												
												
												
											}	
											
											
											
										}	
									}	
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	return validate;
	
}


function getLines(id){
	return $(id).val().split("\n").length
}

function maxTextarea(field){
	
	$(field).limit('150','.characters');
	
	var allowedNumberOfLines = 3;
	if(getLines(field) > allowedNumberOfLines){
		modifiedText = $(field).val().split("\n").slice(0, allowedNumberOfLines);
		$(field).val(modifiedText.join("\n"));
	}	
	
};


