jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};

/*  Added by C. Chen, NGC, Dec 2011 for GA code */
function parseSearch(terms)
{
    var makePipe=true;
    var str = terms.split("");
    var mySearchTerms=new Array();
    for (x in str){
        if (str[x]==" " && makePipe) {
            str[x] = "|";
        } else if (str[x]==' ' && !makePipe) {
            str[x] = "-";
        } else if (str[x]=='"') {
            makePipe = (makePipe) ? false:true;
            str[x] = "";
        }
    }
    var finalStr = str.join("");
    finalStr = finalStr.replace("||","|");
    finalStr = finalStr.replace(/^\|/g,"");
    finalStr = finalStr.replace(/\|$/g,"");
    return finalStr;
}

function OnSubmitForm(lang,formName)
{
  if (formName=='site') {
  
	if (lang=='en-ca'){
		document.site.action ="/en/search?ga_search=";
	} else {
		document.site.action ="/fr/recherche?ga_search=";
	}
	
	document.site.action = document.site.action + parseSearch(document.site.as_q.value+' '+document.site.as_epq.value+' '+document.site.as_oq.value) + "&ga_category=Entire-Site";
  
  } else if (formName=='collections') {

	if (lang=='en-ca'){
		document.collections.action ="/en/search?ga_search=";
	} else {
		document.collections.action ="/fr/recherche?ga_search=";
	}
	
	document.collections.action = document.collections.action + parseSearch(document.collections.artist.value+' '+document.collections.number.value+' '+document.collections.title.value) + "&ga_category=Artists-Artworks";
  
  } else if (formName=='calendar') {

	if (lang=='en-ca'){
		document.calendar.action ="/en/search?ga_search=";
	} else {
		document.calendar.action ="/fr/recherche?ga_search=";
	}
	
	document.calendar.action = document.calendar.action + parseSearch(document.calendar.title.value+' '+document.calendar.description.value) + "&ga_category=Event-Calendar";
  
  } else {

	if (lang=='en-ca'){
		document.sitesearch.action ="/en/search?ga_search=";
	} else {
		document.sitesearch.action ="/fr/recherche?ga_search=";
	}
	
	document.sitesearch.action = document.sitesearch.action + parseSearch(document.sitesearch.q.value) + "&ga_category=Entire-Site";
  
  }
  
  return true;
}

function preloadImages(arrayOfImages) {
	jQuery(arrayOfImages).each(function(){
		if (typeof(this.src) == String)
		(new Image()).src = this; //jQuery('<img/>')[0].src = this;
	});
}

jQuery(document).ready(function()
{
	/* Roll over images */
	var preload = new Array();
	jQuery('img.roll').each(function (i) {
		//let's preload
		var img = new Image();
		img.src = this.src.replace(/([_-])off([._-])/, '$1on$2');
		preload.push(img);
		jQuery(this).hover(
			function () { // over
				jQuery(this).attr('src',this.src.replace(/([_-])off([._-])/, '$1on$2'));
			},
			function () { // out
				jQuery(this).attr('src',this.src.replace(/([_-])on([._-])/, '$1off$2'));
			}
		);
	});
	
	jQuery('body').removeClass('no-js');
	
	// menu show/hide functions
	var nav_interval_in = null;
	var nav_interval_out = null;
	jQuery('#head .nav li.item').hover(
		function() {
			var self = this;
			clearInterval(nav_interval_out);
			nav_interval_in = setInterval(function() {
				clearInterval(nav_interval_in);
				jQuery('#head .nav li.item').removeClass('open');
				jQuery(self).addClass('open');

				// activate the custom scroll if needed (for events)
				var $scroll = jQuery('.ngcscroll', self);
				if ($scroll.length >= 1) {
					$scroll.each(function(){NGCenv.NGCscrollMain(this);});
				}
			}, 100);
		}, // out
		function() {
			var self = this;
			clearInterval(nav_interval_in);
			nav_interval_out = setInterval(function() {
				clearInterval(nav_interval_out);
				jQuery(self).removeClass('open');
			}, 500);
		}
	);
	
	// internet explorer hover image (display info)
	if (jQuery.browser.msie) {
		// fix the bug in IE 6 with the position relative without height
		jQuery('.image').each(function(){
			jQuery(this).css('height', jQuery('img:first', this).height());
		});
		// adding hover event on image for Internet Explorer
		jQuery('.image').hover(function() {
			jQuery(this).addClass('hover');
		}, function() {
			jQuery(this).removeClass('hover');
		})
	}

	var $scroll = jQuery('.ngcscroll');
		if ($scroll.length >= 1) {
			$scroll.each(function(){NGCenv.NGCscrollMain(this);});
	}
	
	jQuery("a.tooltip, a.info").live('hover click', function(){
		jQuery(this).tipTip({defaultPosition:'top'});
		return false;
	});
	
	// increase/decrease size of the text
	// need to add a coockie for this
	jQuery('#quick-links .font-size small').click(function(e){
		e.preventDefault();
		var current_size = parseInt(jQuery('body').css('font-size'));
		if (current_size >= 9)
			jQuery('body').css('font-size', current_size - 1)
	});
	jQuery('#quick-links .font-size big').click(function(e){
		e.preventDefault();
		var current_size = parseInt(jQuery('body').css('font-size'));
		if (current_size <= 12)
			jQuery('body').css('font-size', current_size + 1)
	});
	
	// collection page
	jQuery('#collection-filter .toggle-filters').click(function(e){
		e.preventDefault();
		jQuery('#collection-filter form.filters').toggle();
	});
	
	// listing mode ----------------------------------------------

	if (jQuery('#collection-list').length === 1) {
		if ( jQuery.cookie('listing-mode') != null ) {
			update_listing_mode(jQuery.cookie('listing-mode'));
		} else {
			update_listing_mode('grid');
		}
	
		// switch listing style
		jQuery('#collection-list .layout img').click(function() {
		
			jQuery(this).parent().find('img').each(function(){
				jQuery(this).attr('src',this.src.replace(/([_-])on([._-])/, '$1off$2'));
			});
		
			update_listing_mode(jQuery(this).attr('class'));
			//jQuery('#collection-list').find('div.list').removeClass('listing grid').addClass(jQuery(this).attr('class'));
		});
	}
	
	function update_listing_mode(mode)
	{
		if (mode != 'listing' && mode != 'grid')
			return;
		
		var icon = jQuery('#collection-list img.'+mode);
		
		if( icon.length !== 0){
			jQuery('#collection-list div.list').removeClass('listing grid').addClass(mode);
			icon.attr('src', icon.attr('src').replace(/([_-])off([._-])/, '$1on$2'));
			jQuery.cookie('listing-mode', mode);
		}
	}
	
	// Exapnd Blocks
	jQuery(".expand-target").hide();
	jQuery("a.expand").click(function(){
		var href=jQuery(this).attr('href');
		jQuery(href).slideDown(300);
		return false;
	});
	
	// calendar page listing
	
	jQuery('#page-calendar .grid-8 li').each(function(){
		var li = $(this);
		li.find('.toggle').click(function(e){
			e.preventDefault();
			
			li.hasClass('open') ? li.removeClass('open'): li.addClass('open');
		})
	})
	
	//People's choice vote
	jQuery("#peoples-choice a.vote").live('click',function(){
		var voteLink = jQuery(this).attr('href');
		$.post(voteLink,{},function(data){
			jQuery("#peoples-choice").html(data);
		});
		return false;
	});
	
	var lang = (location.href.indexOf("/en/") != -1) ? "en" : "fr";
	jQuery("input.search-query, .artist-autocomplete").autocomplete({ source: "/search/artists.php?lang=" + lang });
	jQuery(".datepick-past").datepicker({ changeYear: true, yearRange: '100:', dateFormat: 'yy-mm-dd' });
	jQuery(".datepick-future").datepicker({ changeYear: true, yearRange: ':+10', dateFormat: 'yy-mm-dd' });
	if (lang == "fr") { jQuery(".datepick-past, .datepick-future").datepicker( "option", "monthNames", ['Jan','Fév','Mar','Avr','Mai','Juin','Juil','Août','Sept','Oct','Nov','Déc'] ); }
	if (lang == "fr") { jQuery(".datepick-past, .datepick-future").datepicker( "option", "dayNamesMin", ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'] ); }
	
});
