$j(document).ready(function() {

	// wysiwyg hack
	$j('a.link_blank').attr('target', '_blank');
	$j('img.align_right').attr('align', 'right');
	$j('img.align_left').attr('align', 'left');
	
	$j('ul#univert_succursales-ct').hide();
	
	$j('a#univert_succursales_expand').click(function(){
		//console.log($j('ul#univert_succursales-ct'));
		
		$j('ul#univert_succursales-ct').slideToggle('slow');
	});
	
	// Init preferred stylesheet
	initDocument();
	
	// FancyBox
	$j("a.fancy_box_image").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	300, 
		'speedOut'		:	300, 
		'overlayShow'	:	false
	});
	
	$j(".video").click(function() {
		$j.fancybox({
			'padding'		: 15,
			'transitionIn'	:	'fade',
			'transitionOut'	:	'fade',
			'speedIn'		:	300, 
			'speedOut'		:	300,
			'autoScale'		: false,
			'width'			: 768,
			'height'		: 600,
			'href'			: this.href,
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'flashvars'		: $j(this).attr('flashvars'),
			'base'			: $j(this).attr('base'),
			'scale'			: 'noscale',
			'quality'		: 'high',
			'salign'		: 'lt'
			}
		});

		return false;
	});
	
	$j(".toggleBoxIn").click( 
        
	    /* hover */
	    function (e) {
	        e.preventDefault();
	        $j(this).next().show();
	    });

	$j(".toggleBoxOut").click( 
        
        /* hover */
        function (e) {
            e.preventDefault();
            $j(this).parent().hide();
        });

	
});

function isValidPostalcode(postalcode) {
    if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
    else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return true;
    else return false;
}

var locator_postal_code_submit = function(obj)
{
	url = $j(obj).attr('href');
	postal_code = $j('#postal_code_field').val();
	
	if(isValidPostalcode(postal_code))
	{
		
		dist = $j('#distance').val();
		
		url = url.replace("JJJJJJ", postal_code.replace(/ /g,''))
		
		if( ( dist * 1 ) > 0 )
		{
			url = url + dist + "/"
		}
		
		document.location.href = url;
	}else
	{
		$j('#postal_code_field').css('border-color', 'red')
	}
}

function switchStyleState(which){
	if(document.tool0){
		styleState=which;
		if(styleState){
			document.tool0.src=document.tool0.src.replace("_ov","_of");
			document.tool1.src=document.tool1.src.replace("_of","_ov");
			createCookie("style","1",365);
		}else{
			document.tool0.src=document.tool0.src.replace("_of","_ov");
			document.tool1.src=document.tool1.src.replace("_ov","_of");
			createCookie("style",null,365);
		}
	}
}

function setActiveStyleSheet(title){
	if(title=="Default"||title==null||title=="null"){
		switchStyleState(0);
	}else{
		switchStyleState(1);
	}
	var i,a,main;
	for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
		if(a.getAttribute("rel").indexOf("style")!=-1&&a.getAttribute("title")){
			a.disabled=true;
			if(a.getAttribute("title")==title)
				a.disabled=false;
		}
	}
}

function initDocument(){
	cookie=readCookie("style");
	if(cookie=="null")
		cookie='Default';
	title=cookie?cookie:getPreferredStyleSheet();
	setTimeout("setActiveStyleSheet(cookie);",100);
	setTimeout("checkTitle();",100);
}

function createCookie(name,value,days){
	if(days){
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires="; expires="+date.toGMTString();
	} else {
		expires="";
	}
	document.cookie=name+"="+value+expires+"; path=/";}

function readCookie(name){
	var nameEQ=name+"=";
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++){
		var c=ca[i];
		while(c.charAt(0)==' ')
			c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function getActiveStyleSheet(){
	var i,a;
	for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
		if(a.getAttribute("rel").indexOf("style")!=-1&&a.getAttribute("title")&&!a.disabled)
			return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet(){
	var i,a;
	for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
		if(a.getAttribute("rel").indexOf("style")!=-1&&a.getAttribute("rel").indexOf("alt")==-1&&a.getAttribute("title"))
		return a.getAttribute("title");
	}
	return null;
}

function checkTitle(){
	li=document.title.lastIndexOf("#");
	if(li>0){
		document.title=document.title.substring(0,li);
	}
}


