function addLoadEvent(func) {
    $(document).ready(func);
}  

function addUnLoadEvent(func) {
    $(window).unload(func);
}
            
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 swapimg(targetID,newURL) {
	var target = document.getElementById(targetID);
	target.src = newURL;
	return false;
}

$(document).ready(function(){
    
    $('a.hero_trigger').click(function(e){
        var hero = $('img#hero');
        hero.css('opacity','0.5');
        var img = document.createElement('img');
        $(img).load(function(){
            hero.attr('src', img.src);
            hero.attr('width', img.width);
            hero.attr('height', img.height);
            hero.css('opacity','1.0');
        });
        img.src = this.href;
        $(img).attr('title', $(this).attr('title'));
        $(img).attr('alt', $(this).attr('title'));        
        return false;
    });
    
	// this doesn't work
    //$('div.truncatable').truncate({paragraphs: 2});
    
    $('input#search').focus(
        function () {
            if (this.value == 'search') {
                this.value = '';
            }
        }
    );
        
    $('select.country').change (
        function () {
            if (this.value == 'US' || this.value == 'UM') {
                $(this).closest('fieldset').find('div.us_state').css('display','block');
            } else {
                $(this).closest('fieldset').find('div.us_state').css('display','none');
            }
        }
    );
    
    $('select#country').change();


	/* EH */
	
	$('#productnav > ul > li').each(function(){
		if (!$(this).hasClass('open')){
			$(this).addClass('closed');
		}
	});
	
	$('#productnav > ul > li .opens').each(function(){
		$(this).click(function(){
			$(this).parents('li').toggleClass('closed');
			$(this).parents('li').toggleClass('open');
		});
	});
	
	$('#searchField .searchfield').focus(function(){
		if ($('#searchField .searchfield').val()==$('#searchField .searchfield').attr('title')){
			$('#searchField .searchfield').val('');
			$('#searchField .searchfield').addClass('userinput');
		}
		
	});

	$('#searchField .searchfield').blur(function(){
		if ($('#searchField .searchfield').val()==''){
			$('#searchField .searchfield').val($('#searchField .searchfield').attr('title'));
			$('#searchField .searchfield').removeClass('userinput');
		}
	});
	
	// Showroom gallery switcher
	if ($('#content').hasClass('showroom')){
		$('.images li img').click(function(){
			$('#hero').attr('src',$(this).attr('rel'));
		}).addClass('clickable');
	}

	/* customization */
	$('.ProductTinyImageList').each(function() {
		$('.TinyOuterDiv a').each(function() {
			var data = $.parseJSON(this.rel);
			var link = $('#product-image-big a').get(0);
			var img = $('#product-image-big img').get(0);
			$(this).bind('mouseover', function(e) {
				link.href = data.largeimage;
				img.src = data.smallimage;
				img.alt = data.description;
				img.title = data.description;
				//console.dir({d: data, l: link, i: img.src});
				e.preventDefault();
			});
		});
	});
	if(document.getElementById('product-image-big')) {
		$('#product-image-big a').fancybox();
	}

	(function($) {
		$('#LeftMenu .colapsable').each(function() {
			var self = this;
			$('a.toggle', this).bind('click', function(e) {
				$('.childs', self).toggle();
				e.preventDefault();
			});
		});
		if(document.location.href.indexOf('/brands/') != -1) {
			$('#LeftMenuBrands .childs').show();
		}
		if(document.location.href.indexOf('/categories/') != -1) {
			$('#LeftMenuCategories .childs').show();
		}
	})(jQuery);
		
});
