(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function globalMakeTransition(num, limit){
	$('.fader').eq(num).fadeOut(6000);
	if(num < limit -1){
		$('.fader').eq(num+1).fadeIn(6000);
		setTimeout("globalMakeTransition("+(num+1)+", "+limit+");", 12000);
	}else{
		$('.fader').eq(0).fadeIn(6000);
		setTimeout("globalMakeTransition("+0+", "+limit+");", 12000);	
	}
}
function globalInitializeTransition(){
	
	if($('.fader').size() > 1){
		$('.fader:gt(0)').hide();
		setTimeout("globalMakeTransition(0, "+$('.fader').size()+");", 12000);
	}
}


function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(10);
			$('#dio-lens').hide();
			$('#dio-sensor').hide();
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
			$('#dio-lens').show();
			$('#dio-sensor').show();
		});
	
}

 function hoverize(id, all_browsers){
	 
	 if($(id).size()){
		 	jQuery.each($(id), function(i, val){
		 		if(all_browsers == 1){
			 		$(val).hover(
						    function () {
					    	  $(this).css('background-position', 'center bottom');
					        }, 
					        function () {
					        	$(this).css('background-position', 'center top');
					        }
					    );
			 	}else{
			 		if(jQuery.browser.msie && (jQuery.browser.version == 6 || jQuery.browser.version == 7)){
			 			$(val).hover(
							    function () {
						    	  $(this).css('background-position', 'center bottom');
						        }, 
						        function () {
						        	$(this).css('background-position', 'center top');
						        }
						    );

					}
			 	}
		 	});
		 	
			
		 }
 }
 
 $(document).ready(function(){	
	 
	 $('#delivery_extra input').click(function(){
		 $('#delivery_extra').submit(); 
		
	 });
	 
	 $('a.back_button').click(function(){
	        parent.history.back();
	        return false;
	    });
	 
	 $('.search').click(function(){
		//alert();
		if($(this).val() == 'SEARCH'){
			$(this).val('');
		}
	 });
	 
	//mainmenu();
	//$('.zhelp').bbgiframe();
	//$(".second_level").css("z-index","9999");
	//$('#subcategories').hide();
	 //hoverize('#login_submit', 1);
	 //hoverize('#register_submit', 1);
	 hoverize('.hover', 1);
	 hoverize('#add_to_basket', 0);
	 
	 $('.simple_button').button();
	 $('#login_submit').button();
	 $('#register_submit').button();
	 $('#small_news_sub_2').button();
	 
	 //$('#small_news_sub').button();
	 $('.remove').button({ icons: {primary:'ui-icon-closethick'},text: false });
	 $('.edit_icon').button({ icons: {primary:'ui-icon-edit'},text: true });
	 
	 
	 globalInitializeTransition();
	 
	 if($('#page_content').height() < 440){
		 $('#page_content').height(440);
	 }
	 $('#right_menu').height($('#page_content').outerHeight());
	 $('#right_menu_container').height($('#page_content').outerHeight());
	 $('#main_menu').height($('#page_content').outerHeight());
	 
	 

	 $("#subcategories a").mouseenter(function(){
	        $(this).children('h3').css('color', '#5982dc');
	    }).mouseleave(function(){
	    	$(this).children('h3').css('color', '#2A335A');
	    });
	 $(".recently_added_box a img").mouseenter(function(){
	        $(this).css('border', '1px solid #5982dc');
	    }).mouseleave(function(){
	    	$(this).css('border', '1px solid #CDD0D9');
	    });

	 $(".product_container a img,.product_container_right a img").mouseenter(function(){
	      $(this).css('border', '1px solid #5982DC');
	    }).mouseleave(function(){
	    	$(this).css('border', '1px solid #C9CFDA');
	    });

	 
	 
	//563
		//$(document).pngFix(); 
		//jQuery.preLoadImages("/images/menu_top_active_l.gif", "/images/menu_top_active_r.gif", "/images/menu_top_active_bg.gif");
		

});

function handleRegisterAddress(){
	if($("#billing_address").val() == 0){
		$('#billing_address').parent().parent().nextAll(':lt(6)').hide();
	}
	$("#billing_address").change(function(event){
		event.preventDefault();
		if($("#billing_address").val() == 1){
			$('#billing_address').parent().parent().nextAll(':lt(6)').show();
		}
		if($("#billing_address").val() == 0){
			$('#billing_address').parent().parent().nextAll(':lt(6)').hide();
		}
	});
	//alert($("#billing_address").val());
}
function is_valid_email (email)
{
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}
