/*
	JavaScript Functions for brandSAMPLER
	by Ben Hung <benjamin.hung@bamstrategy.com>
	Last updated: 2010-08-10
*/

$(document).ready(function() {


/////////////////////
// country modal  //
///////////////////

// if no cookie, show the country modal
if ($.cookie('countrymodal') != '1') {
	$('#popup').jqm({modal: true});
	$('#popup').jqmShow();
}

// set cookie when button clicked on country modal
$('#popup .button').click(function() {
	$.cookie('countrymodal','1'); // if expires is needed: $.cookie('countrymodal','1',{expires: 7}); expires value is in days
});


if ($.cookie('ssipopup') != '1' && $.cookie('countrymodal')== '1'   ) {
	$('#mainpopup').jqm({modal: true});
	$('#mainpopup').jqmShow();
}

$('#mainpopup .closessi').click(function() {
	$.cookie('ssipopup','1'); // if expires is needed: $.cookie('countrymodal','1',{expires: 7}); expires value is in days
	$('#mainpopup').jqmHide();
});


///////////////////
// common stuff //  Deprecated as of BS6
/////////////////

// border-radius for IE
//$(".border-radius").borderRadius();

// adds background image with rounded border & drop shadow background around TAF form for IE
//if ($.browser.msie) {
//	$('#form_taf').addClass('taf-bg-ie');
//}

// animated popup for sample newsletters
//$('a.photo').fancyZoom({scaleImg: true, closeOnClick: true});
//$('#sample').click(function() {return false;});


////////////////
// #reg page //
//////////////

// province-specific under age of majority check
$('#reg form.prov-underage #submit').click(function() {
								 
	document.getElementById('submit').disabled = 'disabled';
			
	var mmCheck    = $('#sel_mm').val();
	var yyyyCheck  = $('#sel_yyyy').val();
	var provCheck  = $('#sel_province').val();
	var emailCheck = $('#txt_email').val();
	// if the mm, yyyy and province fields are not empty AND if #hidden_consent field != 1
	if( (mmCheck != '') && (yyyyCheck != '') && (provCheck != '') && (emailCheck != '') && $('#hidden_consent').val() != 1 ) {
		if( jsNeedParent(mmCheck,yyyyCheck,provCheck) == true) {  // if underage, show parent consent modal
			showUnderProv();
			return false;
		} else {  // else submit the form (will be validated by action)
			$('form').submit();
		}
	}
	// if any of the mm, yyyy and province fields are empty OR if #hidden_consent field = 1
	else {
		$('form').submit();	
	}
	
});

// if mm, yyyy or province fields change, reset the #hidden_consent field
$('#sel_mm, #sel_yyyy, #sel_province').change(function() {
	$('#hidden_consent').val('');
});

// prov underage: capture ENTER key to prevent submit
$('#parental-consent').bind('keypress', function(e) {
	if(e.keyCode==13){
		return false;	
	}
});

////////////////////
// #samples page //
//////////////////

// on page load, adds checked state display to samples that have been previously checked
$('.Prod_block').find('input:checked').parent().removeClass('off').addClass('on');

// PANTENE TOOL: on mouseclick of Pantene header area, open Pantene Tool if unchecked, or uncheck if already checked
$('#samples .Pantene_header').click(
	function() {
		if($(this).hasClass('off')) {
			// blinks #pantene-tool-link
			//$('a#pantene-tool-link, a#pantene-tool-link + img').stop(true,true).fadeIn(150).fadeOut(150).fadeIn(150).fadeOut(150).fadeIn(150);
			$('#pantene-tool').jqmShow();
		} else {
			$(this).find('input').attr('checked','');
			$(this).find('input').attr('value','');
			$(this).removeClass('on');
			$(this).addClass('off');
		}
});

// PANTENE TOOL: launches Pantene tool modal
$('#pantene-tool').jqm({
	ajax: 'sample.pantene.php',
	trigger: '#pantene-tool-link',
	overlay: 50,
	modal: true
});  

// On mouseclick of header area, toggles background state and checks checkbox appropriately
$('#samples .Prod_header').click(
	function() {
		if($(this).hasClass('off')) {
			$(this).removeClass('off');
			$(this).addClass('on');
			$(this).find('input').attr('checked','checked');
		} else {
			$(this).removeClass('on');
			$(this).addClass('off');
			$(this).find('input').attr('checked','');
		}
	}
);

// On mouseclick of header area, toggles background state and selects radio buttons appropriately
$('#samples .cat-prod-header').click(
	function() {
		if($(this).hasClass('off')) {
			$(this).parent().parent().find('.cat-prod-header').removeClass('on');
			$(this).parent().parent().find('.cat-prod-header').addClass('off');
			$(this).removeClass('off');
			$(this).addClass('on');
			$(this).parent().parent().find('input').attr('checked','');
			$(this).find('input').attr('checked','checked');
		} else if($(this).hasClass('on')) {
			$(this).removeClass('on');
			$(this).addClass('off');
			$(this).find('input').attr('checked','');
		}
	}
);

});

$(document).keydown( function( e ) { 
   if( e.which == 27) {  // escape, close box 
   	 $('#mainpopup').jqm({modal: true});
     $('#mainpopup').jqmHide();
   } 
 }); 

