jQuery(document).ready(function(){		
		
		AarfieGlobalBind();
		
		jQuery('.images_nav a').click(function(event){
			event.preventDefault();
			var theClick = jQuery(this);
			var navWRAP = jQuery(this).closest('.images_nav');
			var imgWRAP = jQuery('.image_wrap').first();
			if(imgWRAP.hasClass('use_ind')) {
				var clickIND = jQuery(this).attr('data-imgind');
				var activeDIV = jQuery('.image_wrap').first().find('.active').first();
				var activeIND = activeDIV.attr('data-imgind');
				if(clickIND != activeIND) {
					activeDIV.fadeOut(100, function(){
						jQuery(this).removeClass('active');
						imgWRAP.find('.product_img').removeClass('active');
						imgWRAP.find('.product_img[data-imgind="'+ clickIND +'"]').first().fadeIn(200, function(){
							jQuery(this).addClass('active');
							navWRAP.find('a').removeClass('nav_active');
							theClick.addClass('nav_active');
						});
					});
				}
			}
			else {
				var clickHREF = jQuery(this).attr('href');
				var activeDIV = jQuery('.image_wrap').first().find('.active').first();
				var activeHREF = activeDIV.find('img').first().attr('src');
				if(clickHREF != activeHREF) {
					activeDIV.fadeOut(200, function(){
						jQuery(this).removeClass('active');
						imgWRAP.find('.product_img').removeClass('active');
						imgWRAP.find('img[src="'+ clickHREF +'"]').first().closest('.product_img').fadeIn(200, function(){
							jQuery(this).addClass('active');
							navWRAP.find('a').removeClass('nav_active');
							theClick.addClass('nav_active');
						});
					});
				}
			}		
		});
	
		jQuery('.images_nav #images_next_nav').click(function(event){
			event.preventDefault();
			var navWRAP = jQuery(this).closest('.images_nav');
			var imgWRAP = jQuery('.image_wrap').first();
			var activeDIV = jQuery('.image_wrap').first().find('.active').first();
			var nextDIV = activeDIV.next('.product_img');
			var nextDIVURL = activeDIV
			if(isEmpty(nextDIV)) nextDIV = imgWRAP.find('.product_img').first();
			if(! isEmpty(nextDIV)) {
				activeDIV.fadeOut(100, function(){
					jQuery(this).removeClass('active');
					imgWRAP.find('.product_img').removeClass('active');
					nextDIV.fadeIn(200, function(){
						jQuery(this).addClass('active');					
						if(imgWRAP.hasClass('use_ind')) {
							var nextIND = nextDIV.attr('data-imgind');
							navWRAP.find('a').removeClass('nav_active');
							if(! isEmpty(nextIND) ) navWRAP.find('a[data-imgind="'+ nextIND +'"]').first().addClass('nav_active');
						} else {
							var nextHREF = nextDIV.find('img').first().attr('src');
							navWRAP.find('a').removeClass('nav_active');
							if(! isEmpty(nextHREF) ) navWRAP.find('a[href="'+ nextHREF +'"]').first().addClass('nav_active');
						}
					});
				});
			}
		});	
		jQuery('.images_nav #images_prev_nav').click(function(event){
			event.preventDefault();
			var navWRAP = jQuery(this).closest('.images_nav');
			var imgWRAP = jQuery('.image_wrap').first();
			var activeDIV = jQuery('.image_wrap').first().find('.active').first();
			var nextDIV = activeDIV.prev('.product_img');
			if(isEmpty(nextDIV)) nextDIV = imgWRAP.find('.product_img').last();
			if(! isEmpty(nextDIV)) {
				activeDIV.fadeOut(100, function(){
					jQuery(this).removeClass('active');
					imgWRAP.find('.product_img').removeClass('active');
					nextDIV.fadeIn(200, function(){
						jQuery(this).addClass('active');					
						if(imgWRAP.hasClass('use_ind')) {
							var nextIND = nextDIV.attr('data-imgind');
							navWRAP.find('a').removeClass('nav_active');
							if(! isEmpty(nextIND) ) navWRAP.find('a[data-imgind="'+ nextIND +'"]').first().addClass('nav_active');
						} else {
							var nextHREF = nextDIV.find('img').first().attr('src');
							navWRAP.find('a').removeClass('nav_active');
							if(! isEmpty(nextHREF) ) navWRAP.find('a[href="'+ nextHREF +'"]').first().addClass('nav_active');
						}
					});
				});
			}
		});
		
		jQuery('.other_product_thumb a.thumb_link').not('.other_product_thumb.no_roll a.thumb_link').hover(
		function(event){
			event.preventDefault();
			var divIMG = jQuery(this).find('.shop_thumb_img_holder').first();
			var divTEXT = jQuery(this).find('.make_table_outer').first();
			divTEXT.hide(0);
			divIMG.css('display', 'block').fadeOut(100, function(){
				divTEXT.css('display', 'block').fadeTo(0, 0.01, function(){
					jQuery(this).fadeTo(100, 1);
				});
			});
		},
		function(event){
			event.preventDefault();
			var divIMG = jQuery(this).find('.shop_thumb_img_holder').first();
			var divTEXT = jQuery(this).find('.make_table_outer').first();
			divIMG.hide(0);
			divTEXT.css('display', 'block').fadeOut(100, function(){
				divIMG.css('display', 'block').fadeTo(0, 0.01, function(){
					jQuery(this).fadeTo(100, 1);
				});
			});
		}
		);
		
		jQuery('td.item_options .remove_btn').bind('click', function(event){
			event.preventDefault();
			jQuery(this).closest('td').siblings('.item_qty').first().find('input').first().val('0');
			jQuery('#cart_form').submit();
		});
		
		jQuery('button.checkout_btn').bind('click', function(event){
			event.preventDefault();
			jQuery('#cart_form').attr('action', '/cart?checkout=1');
			jQuery('#cart_form').submit();
		});
		
		var IEv = getInternetExplorerVersion();
		if(!isEmpty(IEv) && IEv != -1 && IEv <= 7) {
			jQuery('#products .shop_thumb_img_holder img').click(function(){
				var linkURI = jQuery(this).closest('a').attr('href');
				if(!isEmpty(linkURI)) location.href = linkURI;
			});
		}
		
		var canonicalURI = jQuery('link[rel="canonical"]').first().attr('href'); 
		if( !isEmpty (canonicalURI) ) {
			if(canonicalURI.search("http://preview.aarfie.com/bc/") != -1) { AarfiePreviewSetup(); }
		}
		
});

function AarfieGlobalBind() {
				 		
		jQuery('.hide-if-no-js').show();
		jQuery('.hide-if-js').fadeOut(200, function(){
				jQuery(this).hide();
		});
				
		jQuery("a[href^='#']").unbind('click').bind('click', function(event) { 
			event.preventDefault();
			
			var anchornameraw = jQuery(this).attr("href");
			var anchorname = anchornameraw.substr(1);
			var istofootnote = jQuery("#" + anchorname).closest("div").hasClass("footnote");
			if (jQuery("a[name=" + anchorname + "]").length) {
				scrolltopanchor = jQuery("a[name=" + anchorname + "]").offset().top;
			} else {
				scrolltopanchor = jQuery("#" + anchorname).offset().top;
			}
			if (anchorname != "pagetop") {
				scrolltopanchor = scrolltopanchor - 30;
			}
			
			jQuery("html, body").animate({
				scrollTop: scrolltopanchor
				}, 800);
						    	
			return false;
			
		});
		
		var formJar = new Array();
		jQuery('form').each(function(i) {
			jQuery(this).find('input[type="text"]').each(function(k) {
				formJarIndex = i + '-' + k;
				jQuery(this).attr('data-ind', formJarIndex);
				formJar[formJarIndex] = jQuery(this).val();
			});
		});
				
		jQuery('input[type="text"]').unbind('click').bind('click', function() {
			var formJarIndex = jQuery(this).attr('data-ind');
			if(formJarIndex) {
				if(jQuery(this).val() == formJar[formJarIndex]) jQuery(this).val('');
			}
		});
		jQuery('input[type="text"]').unbind('blur').bind('blur', function() {
			var formJarIndex = jQuery(this).attr('data-ind');
			if(formJarIndex) {
				if(! jQuery(this).val().length ) jQuery(this).val(formJar[formJarIndex]);
			}
		});
		
		var cookieCheck = jQuery.cookies.get('aarfiecookiecheck');
		if(cookieCheck != 'yes') {
			jQuery.cookies.set('aarfiecookiecheck', 'yes');
			cookieCheck = jQuery.cookies.get('aarfiecookiecheck');
			if(cookieCheck != 'yes') {
				jQuery('#wrap').before('<div class="public_notice" style="background-color: #cc0000;"><div class="notice_inner"><strong>Please enable Cookies on your web browser to use this web site!</strong></div></div>');	
			}
		}
		
		jQuery('#footer').find('a[title*="Visit Aarfie"]').each(function(){
			var art = jQuery(this).html();
			var nart = art.replace('Aarfie', 'Aarcade');
			if(art != nart) {
				jQuery(this).html(nart).attr('href', 'http://www.aarcadethemes.com/').attr('title', 'Visit Aarcade');
			}					
		});
		
}

function isEmpty (mixed_var) {
    var key; 
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || typeof mixed_var === 'null' || typeof mixed_var === 'undefined' || mixed_var.length === 0) { return true; } 
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) { return false; }
        return true;
    }
    return false;
}

function setupColorBox () {
    var colorBoxScript = jQuery('script[src*="colorbox-min.js"]');
    var aarfieStoreScript = jQuery('script[src*="aarfie-store.js"]');
    if(isEmpty(colorBoxScript) && ! isEmpty(aarfieStoreScript)) {
   		aarfieStoreScriptSrc = aarfieStoreScript.attr("src");
    	aarfieStoreScript.after("<script type='text/javascript' src='"+ aarfieStoreScriptSrc.replace("/aarfie-store.js", "/colorbox-min.js") +"'></script>");
    }
    var colorBoxScript = jQuery('script[src*="colorbox-min.js"]');
    if(!isEmpty(colorBoxScript) && ! isEmpty(aarfieStoreScript)) {
   		aarfieStoreScriptSrc = aarfieStoreScript.attr("src");
    	colorBoxScript.after("<script type='text/javascript' src='"+ aarfieStoreScriptSrc.replace("/aarfie-store.js", "/global-colorbox.js") +"'></script>");
    }
}

function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function AarfiePreviewSetup() {
		
	jQuery('td.item_options .remove_btn').unbind('click').bind('click', function(event){
		event.preventDefault();
		AarfiePreviewAlert();
	});
	
	jQuery('button.checkout_btn').unbind('click').bind('click', function(event){
		event.preventDefault();
		AarfiePreviewAlert();
	});
			
	jQuery('form').unbind('submit').bind('submit', function(event){
		event.preventDefault();
		AarfiePreviewAlert();
		return false;
	});

}

function AarfiePreviewAlert() {
	alert('This function is not enabled for Previews');
}
