//define global variables
var currentFanclubItems = [];	
var popupStatus = 0;
var currentAsin = '';
var fanclub_user_hash = '';
var validate_from_popup = '0';
var disabledLinks = [];


//set escape key for popup close
$(document).keypress(function(e){  
 	if(e.keyCode==27 & popupStatus==1){  
 		disablePopup();  
 	}  
}); 



$(document).ready(function() {



 	var rand_no = Math.floor(Math.random()*5);

	//if they're validated, don't bother
	if($.cookie('fanclub_member') == 'valid') { 
		return false;
	}

	//set up popup form
	var popup_form = document.createElement("div");
	popup_form.setAttribute("id", "popup_form");
	$("body").append(popup_form);
	$("body #popup_form").css('z-index','100');


	var popup_background = document.createElement("div");
        popup_background.setAttribute("id", "popup_background");
	$("body").append(popup_background);


	//hide buy button
	//$('#product_sub_info .option a').hide();
	
	//hide sizes
	//$('.type .type').hide();

	
	

	//hide buy button for product
	//$('.dd .wba_add_to_cart_btn').hide();
	//$('#ClothingSize').hide();

	//hide buy button if it exists
/*	if($('.wba_add_to_cart_btn').parent().attr('href') != null){
	
	 	for ( var i in fanclubAsins ){
		

			if($('.wba_add_to_cart_btn').parent().attr('href').indexOf(fanclubAsins[i]) > 0) {

				$('.wba_add_to_cart_btn').hide();
			}
		}
	}
*/



	//product page with sizes
	if (forbiddenProduct()) {

		
		$('#col_498 .section a').each(function(index) {
			disabledLinks[index] = $(this).attr('href');
			$(this).css('cursor','default');
			$(this).attr('href','');
			$(this).click(function(e) {
			    e.preventDefault();
			    //do other stuff when a click happens
			});
			
			//$(this).removeAttr('href');
			//create form container
	        
		
		});
		var fanclub_form_container = document.createElement("div");
        fanclub_form_container.setAttribute("id", "fanclub_form_container");
	 	$(fanclub_form_container).insertBefore($('#col_498 .section'));
		setup_fanclub_container();
		return false;
	}



	//sizes first
	$('#col_498 .info span a').each(function(index) {
		for ( var i in fanclubAsins ){
			
			var theAsin = get_link_asin(this.href);
			var theAsin2 = get_link_asin2(this.href);
			
			if(fanclubAsins[i] != theAsin) {
				if(fanclubAsins[i] == theAsin2) {
					theAsin = theAsin2;
				}
			}
			
			if(fanclubAsins[i] == theAsin) {
				//alert('found it!');
				$(this).addClass('fanclub');
				$(this).attr("id", theAsin);
				//$(this).parent().addClass('fanclubParent');
				
				var productNameFull = $(this).parent().parent().children().eq(0).text();
				var productName = productNameFull.replace('(','');
				productName = productName.replace(')','');
				productName = productName.replace(' ','');
				productName = productName.replace('-','');
				
				
				newLink = '/product/' + theAsin + '/' + productName;
				
				
				currentFanclubItems[theAsin] = newLink;

		
				$(this).click( function() { 
					purchase_fanclub_item($(this).attr('id'));
				})
				$(this).removeAttr('href');
			}
		}
	});

	
	//categories, home
	$('#col_498 .info a, .html_include a').each(function(index) {
		for ( var i in fanclubAsins ){
			
			
			var theAsin = get_link_asin(this.href);
			var theAsin2 = get_link_asin2(this.href);
			
			if(fanclubAsins[i] != theAsin) {
				if(fanclubAsins[i] == theAsin2) {
					theAsin = theAsin2;
				}
			}
			
			if(fanclubAsins[i] == theAsin) {
				
				//alert('found it!');
				$(this).addClass('fanclub');
				$(this).attr("id", theAsin);
				//$(this).parent().addClass('fanclubParent');
				currentFanclubItems[theAsin] = this.href;
				
				
		
				$(this).click( function() { 
					purchase_fanclub_item($(this).attr('id'));
				})
				$(this).removeAttr('href');
			}
		}
	});
	


	



	
	
	//buy button
	$('#product_sub_info .option a').each(function(index) {
	
		for ( var i in fanclubAsins ){
			
			var theAsin = get_link_asin(this.href);
			var theAsin2 = get_link_asin2(this.href);
			
			if(fanclubAsins[i] != theAsin) {
				if(fanclubAsins[i] == theAsin2) {
					theAsin = theAsin2;
				}
			}
			
			if(fanclubAsins[i] == theAsin) {
				//alert('found it!');
				
				$(this).parent().addClass('addToCart');
				$(this).parent().hide();
				
				
				//create form container
		        var fanclub_form_container = document.createElement("div");
		        fanclub_form_container.setAttribute("id", "fanclub_form_container");
			 	$(fanclub_form_container).insertBefore($('#product_info .bio p'));
				setup_fanclub_container();
		
				//$(this).click( function() { 
//
//					purchase_fanclub_item(theAsin);
//
//				})
				
			}
		}
	});
	
	
	
	
	$('.fanclubParent a').each(function(index) {
		$(this).removeAttr('href');
	});



	
	// ******************************************************
	// for single product pages
	// ******************************************************

	//check product asins
	var current_asin = get_page_filename(top.location.href);

	//check to see if this page is for an item that is on the fanclub list
	for ( var i in fanclubAsins )
	{
		if(fanclubAsins[i] == current_asin) {
			var is_fanclub_item = 1;
			break;
		}
	
	} 

	//we have a winner!  proceed
	if(is_fanclub_item == 1) {
		//create form container
        var fanclub_form_container = document.createElement("div");
        fanclub_form_container.setAttribute("id", "fanclub_form_container");
	 	$(fanclub_form_container).insertAfter($('#addToCartErrorMessage'));
		setup_fanclub_container();
	} else {
	//show the buy button
		$('.dd .wba_add_to_cart_btn').show();
		$('#ClothingSize').show();

	}


})


