$(document).ready(function(){
	
/*  -------------------------------------------------------------------
	WebTrends --------------------------------------------------------- */
	$("#b_facebook, #b_checkitout, .b_facebook").click(function() {
		dcsMultiTrack('DCS.dcsuri','Facebook');							
	});
	
	$(".i_canadian-milk").click(function() {
		dcsMultiTrack('DCS.dcsuri','dairygoodness.ca');				
	});
	
	$("#b_videos").click(function() {
		dcsMultiTrack('DCS.dcsuri','video');							
	});
	
	
/*  -----------------------------------------------------------------------
	External links -------------------------------------------------------- */
    $('a[rel=external]').live('click', function(e){
		e.preventDefault(); 
        open(this.href);     
    });


/*  -----------------------------------------------------------------------
	Video links ----------------------------------------------------------- */
	var flashvars = {};
	var params = {wmode: "transparent", play: "false"};
	var attributes = {};
	$("#video1").click(function() {
		$("#video_player").show();						
		swfobject.embedSWF("/web/flash/video2009.swf", "video_player", "370", "248", "7.0.0", "", flashvars, params, attributes);
		$("#video_player2").hide();
		return false;							  
	});
	$("#video2").click(function() {
		$("#video_player2").show();						
		swfobject.embedSWF("/web/flash/cheese rolling video2.swf", "video_player2", "370", "248", "7.0.0", "", flashvars, params, attributes);
		$("#video_player").hide();
		return false;							  
	});
	
	
/*  -----------------------------------------------------------------------
	Forms Validation ------------------------------------------------------ */
	var reg_empty       = /^\s*$/;
    var reg_email       = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	function TestRegExp(val, pattern){ 
        objRegExp   = new RegExp(pattern);
        return objRegExp.test(val);
    };


/*  -----------------------------------------------------------------------
	Q&A toggle ------------------------------------------------------------ */
	var $dlqa = $("dl.qa");
	if($dlqa.size() > 0) {
		
		//Hide (Collapse) the toggle containers on load
		$("dd", $dlqa).hide(); 
			
		//Slide up and down on click
		$("dt", $dlqa).click(function(){
			if(!$("dd:animated", $dlqa).size() > 0) {
				if(!$(this).hasClass("active")) {
					$("dt", $dlqa).removeClass("active");
					$(this).addClass("active");
					$("dl.qa dd").hide();
					$(this).next("dd:first").slideToggle("fast");
				}else {
					$(this).next("dl.qa dd").slideToggle("fast");
					$(this).removeClass("active");
				}
			}
			return false;
		});
	}


/*  -----------------------------------------------------------------------
	Restaurants expander -------------------------------------------------- */
	$("#z_resto").hide();
	$("#b_restaurants").click(function() {
		$("#z_resto").toggle();
		return false;
	});
	

/*  -----------------------------------------------------------------------
	Cheese makers expander -------------------------------------------------- */
	$("#z_makers").hide();
	$("#b_makers").click(function() {
		$("#z_makers").toggle();
		return false;
	});
	

/*  -----------------------------------------------------------------------
	Gallery --------------------------------------------------------------- */
	if($("body").attr("id")=="newsroom"){
		$('#media_tabs').tabs();
		
		$('#mycarousel').jcarousel({
        	scroll: 2,
			visible: 4
    	});
		
		$('#mycarousel a').click(function() {
			$("#img_wrapper img").attr("src", $(this).attr("href"));
			return false;
		});
	}
	

/*  -----------------------------------------------------------------------
	All stars ------------------------------------------------------------- */
	if($("body").attr("id")=="allstars"){
	
		$('#allstars_carousel').jcarousel({
        	scroll: 1,
			visible: 1
    	});
		
		$('#allstars_carousel a').click(function() {
			el = $(this).attr("href");
			$("div", "#backcard").hide();
			$(el).show();
			return false;
		});
	}


/*  -----------------------------------------------------------------------
	Signup form ----------------------------------------------------------- */
	$("#frm_signup").submit(function() {
		var error = 0;
		$('p.error').remove();
		$('.error').toggleClass("error");

		$('#frm_signup .v-mandatory').each(function() {
			if($(this).attr("type") == "checkbox") {
				if(!$(this).attr('checked')) {
					$(this).next('label:first').addClass('error');
					error = 1;
				}
			}else {
				if($(this).val() == '') {
					$(this).prev('label:first').addClass('error');
					$(this).addClass('error');	
					error = 1;
				}
			}
		});
		
		if(!TestRegExp($("#signup_email").val(), reg_email)) {
			error = 1;	
			$("#signup_email").prev('label:first').addClass('error');
			$("#signup_email").addClass('error');
		}
		
		if(!$("#signup_gender_male").attr('checked') && !$("#signup_gender_female").attr('checked')) {
			$("#label-gender").addClass('error');
			error = 1;
		}

		if(error == 1) {
			$("#frm_signup").prepend("<p class=\"error\">Oops! Please fill in all the <em>required fields</em>.</p>");
		}else {
			$.ajax({
				type: "POST",
				url: "/inc/db_signup.php",
				data: $("#frm_signup").serialize(),
				success: function(msg){
					
					if(msg == 1) {
						$("#frm_signup").before('<div class="message"><span class="inner"><p>Thank you.</p><p>Your registration has been accepted. Remember, you\'ll still need to sign in at the registration tent on the day of the event.</p></span></div>');
						$("#frm_signup").remove();
					}else if(msg == 2) {
						$("#frm_signup").before('<div class="message"><span class="inner"><p>Sorry, all the online spots have been filled.</p><p>50 extra spots will also be available on-site on the day of the event.</p></span></div>');
							
						$("#frm_signup").remove();
						
					}else {
						$("#frm_signup").prepend("<p class=\"error\">Oops! an error occured. Please try again.</p>");	
					}
					// WebTrends
					dcsMultiTrack('DCS.dcsuri','Sign-up');	
				},
				error: function() {
					$("#frm_signup").prepend("<p class=\"error\">Oops! an error occured. Please try again.</p>");		
				}
			});
		}
	
		return false;
	});
	

/*  -----------------------------------------------------------------------
	Send to a friend ------------------------------------------------------ */
	$("#b_stf").click(function(e) {
		e.preventDefault();
	    stf_popin = $.stickyPopin({
			width: 342,
		    opacity: 0.9,
		    modal: false
	    });
	});
	
	$("#frm_stf").submit(function() {
		error = 0;
		$('p.error').remove();
		$('.error').toggleClass("error");
		
		$('#frm_stf .v-mandatory').each(function() {
			if($(this).val() == '') {
				$(this).prev('label:first').addClass('error');
				$(this).addClass('error');	
				error = 1;
			}
		});
		
		$('#frm_stf .v-email').each(function() {
			if(!TestRegExp($(this).val(), reg_email)) {
				error = 1;	
				$(this).prev('label:first').addClass('error');
				$(this).addClass('error');
			}
		});
		
		if(error==0){
			
			$.ajax({
				type: "GET",
				url: "/inc/sendtoafriend.php",
				data: "stf_name="+ escape($("#stf_name").val()) +"&stf_email="+ escape($("#stf_email").val()) +"&stf_friend_name="+ escape($("#stf_fname").val()) +"&stf_friend_email=" + escape($("#stf_femail").val()) +"&link=" + escape($("#stf_page").val()),
				success: function(msg){
					popSTFsucess = '<div class="pi_stfsuccess">';
					popSTFsucess += '		<p>'+ msg +'</p>';
					popSTFsucess += '<a href="javascript:;" onclick="stf_popin.close();" class="b_close">Close</a>';
					popSTFsucess += '</div>';
					$("#frm_stf").before(popSTFsucess);
					$("#frm_stf").hide();
					$("input:text", "#frm_stf").each(function() {
						$(this).val("");
					});
					
					// WebTrends
					dcsMultiTrack('DCS.dcsuri','Send to a friend');
				},
				error: function() {
					$("#frm_stf").prepend("<p class=\"error\">Oops! an error occured. Please try again.</p>");
				}
			});
		}
		
		return false;

	});
	
	
/*  -------------------------------------------------------------------
	Open a popup page ------------------------------------------------- */
	var winReg;
	function popupPage(url, name, x, y){
		options = "height="+y+",width="+x+",toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,left=0,top=0";
		winReg = window.open(url, name, options);
		winReg.focus();
	};

	// Popup links
    $('a[rel=popup]').live("click", function(e){ 
        e.preventDefault();
        popupPage($(this).attr('href'), $(this).attr("rel"), 700, 600); 
    });
	
});