var cart_popup_id = null;
var ready_loaded=false;
var hash = '';
var orig = '';

$(document).ready(function() {
		if(!ready_loaded) {
			attach_tab_clicks();
			attach_add_to_cart_hover();
			attach_cpp_view_contents();
			attach_quick_add_submit();
			ready_loaded=true;
		}

		hash = window.location.hash;
		orig = window.location;
		
		if(hash.length) {
			hash = '';
			hash_change();
		} else {
			setTimeout (hash_change,100);
		}
}); 

function hash_change() {

		if(hash != window.location.hash) {

			hash = window.location.hash;

			if(hash.length != 2) {
                return true;
				//var obj = $('div#party_builder_products ul li a');
				//url = obj.attr('href');
			}
			else {
				var obj = $('a#tag_' + hash.replace('#', ''));
				url = obj.attr('href');
			}
			$('#atc_popup').fadeOut('slow');				
			$('div#party_builder_products_inner_div').html('Party Products Loading...').css('text-align', 'center');
            if(url.search('undefined') == -1) {
                $("#party_builder_products").load(url + 'A',function() {
                        attach_tab_clicks(); attach_quick_add_submit();
                    });	
            } else {
            }
		}
        
		setTimeout (hash_change, 200);	
		return false;
}
function add_to_cart_popup(handler) {

  http_state_changed(handler);
  if (http.readyState==4 || http.readyState=="complete")
  {
  	$("span.loading").hide();
		
  	$("#added_options").fadeIn("slow");

		$('#atc_popup').center();
  	clearTimeout(cart_popup_id);
			
		//if the user has previously added an item to their cart
		//  hide the popup after 3 seconds	
		if(get_cookie('add_to_cart_popup')=='1') {
			cart_popup_id = setTimeout("hide_popup()",3000);
		} else {
			set_cookie('add_to_cart_popup', '1');
		}
			
  }
} 

function hide_popup() {
		$('#atc_popup').fadeOut('slow');
  	return false;
}

function add_to_cart(url, type_id, type) {
  	clearTimeout(cart_popup_id);

		if(type == 'package') {
			
			var qty_select = $("select#quantity_" + type_id);
			qty = 1 + '&' + qty_select.attr('name') + '=' + qty_select.val();
			type_id = type_id.split('_');

			type_id = type_id[0];
			var var_sep = '&';
			
		} else {

			var qty = $("input#quantity_" + type_id).val();		

			if(qty == 0) {
				alert('Please enter a quantity.');
				return false;
			}
			$("input#quantity_" + type_id).val('0');
			var var_sep = '?';


		}
    var url = url + var_sep + 'ajax=1&add=1&qty=' + qty + '&type_id=' + type_id + '&type=' + type;
		error_function = function (error) { $('#atc_error').hide().html('<br>' + error).slideDown('medium')};

    send_custom(url, add_to_cart_popup); 

	if($('#atc_popup').length) {
		var popup_div = $('#atc_popup');
	} else {

        //adding the element with jQuery did not
        // work well in IE 6, so we do it manually
        var new_div = document.createElement("div");
        new_div.id = "atc_popup";
        new_div.className ="";
				var base_dir = $('#base_dir').html();

        new_div.innerHTML = '<div class="hidden_box"><div style="font-size:8px;">&nbsp;</div></div><div class="i1"><div class="i2"><div class="i3" style="padding:6px;"><span class="loading" id="loading"><br><img src="' + base_dir + 'templates/images/waiting.gif"><br><br>Adding Product To Cart<br><br></span><div id="added_options"><span id="cart_added_qty"></span>&nbsp;<span id="cart_added_qty_text"></span> added to your Shopping Cart<br><br><table width="225px;"><tr><td><span id="cart_added_product_image"></span></td><td align="left" style="padding-left:15px;"><span id="cart_added_category_name"></span><br><span id="cart_added_product_name"></span><br><span style="color:#0099CC;">(<span id="cart_added_product_included"></span>&nbsp;<span id="cart_added_product_included_text"></span>)</span></td></tr></table><br><a href="#" onClick="return hide_popup();">Continue Shopping</a><br><br><a href="' + base_dir + 'shopping_cart.php">View Cart</a><br><br><a href="' + base_dir + 'shopping_cart.php">Checkout</a><br><span id="atc_error"></span></div></div></div></div><div class="hidden_box bottom"><div>&nbsp;</div></div>';

        document.body.appendChild(new_div);
				
		var popup_div = $('#atc_popup').appendTo(document.body).hide();
		popup_div.live('mouseover', function() {
					clearTimeout(cart_popup_id);
				}).live('mouseout', function() {
//					cart_popup_id = setTimeout("hide_popup()",1000); 
				});
	}
    $("#added_options").hide();    

		$('#atc_error').hide();
    popup_div.center().fadeIn("medium");
    $("#loading").show();

    return false;
}


function attach_tab_clicks() {

	$("div#continue_to_next_step").live('mouseover', function() {
		img = $(this).children().children().css('color', 'black').filter('img');

		img_src = img.attr('src');
		img.attr('src', img_src.replace('BLUE', 'BLACK'));
	}).live('mouseout', function() {
		$(this).children().children().css('color', '#0099CC');	

		img_src = img.attr('src');
		img.attr('src', img_src.replace('BLACK', 'BLUE'));
	});

	$(".theme_tab").live('click',tab_clicked);
	$(".theme_tab_arrow").live('click', tab_clicked);
	
	$('.theme_tab_arrow').live('mouseover', function() { 
        if(!$(this).next().hasClass('selected')) {
            highlight_arrow($(this));
            highlight_tab($(this).next());
        }
        
    }).live('mouseout', function() {
        unhighlight_arrow($(this));
        unhighlight_tab($(this).next());
    });
    
	$('.theme_tab, .theme_tab_arrow').not('.selected').live('mouseover', function() {
        if(!$(this).hasClass('selected')) {
            highlight_tab($(this));
            
            arrow = $(this).prev();
            highlight_arrow(arrow);
        }
	
	}).live('mouseout', function() {
		unhighlight_tab($(this));
		arrow = $(this).prev();
        unhighlight_arrow(arrow);
	});


}
function get_tab_id(url) {

	var id = url.split('/');
	id = id[id.length-2];

    if(id !== undefined) {
        return id;
    }
    return false;
}


function tab_clicked(obj) {

	if($(this).attr('href')) {
		var url = $(this).attr('href');
	} else {
		var url = $(this).children('a').attr('href');
	}
	var id = get_tab_id(url);
    
    if(id !== false) {
        window.location.hash = id;
        return false;
    }
    return true;
}

function load_products(category_id, tag_id) {
	$()
}

function attach_add_to_cart_hover() {
	$('.add_to_cart').live('mouseover', function() {
		$(this).addClass('hover').css('cursor', 'pointer');
	}).live('mouseout', function() {
		$(this).removeClass('hover');
	});
}

function attach_cpp_view_contents() {
	$('#cpp_view_contents').live('mouseover', function() {
			var path = $(this).attr('src');
			path = path.replace('White', 'Black');
			$(this).attr('src', path);
		}).live('mouseout', function() {
			var path = $(this).attr('src');
			path = path.replace('Black', 'White');
			$(this).attr('src', path);		
		}).click(function() {
			var path = $(this).attr('src');
			path = path.replace('White', 'Black');
			$(this).attr('src', path);		
		});
		
	$('#cpp_star_img').live('mouseover', function() {
			
			var path = $(this).attr('src');
			path = path.replace('Blue', 'Black');
			$(this).attr('src', path);
			
		}).live('mouseout', function() {
			var path = $(this).attr('src');
			path = path.replace('Black', 'Blue');
			$(this).attr('src', path);		
		}).click(function() {
			var path = $(this).attr('src');
			path = path.replace('Blue', 'Black');

			$(this).attr('src', path);		
		});
		
}

function highlight_tab(tab) {
    tab.addClass('black2');
}
function unhighlight_tab(tab) {
    tab.removeClass('black2');
}
function highlight_arrow(arrow) {
    var color = 'WHITE';
    if(arrow.hasClass('selected')) {
        color = 'BLUE';
    }
    arrow = arrow.children().children();
	var img = arrow.attr('src');
	arrow.attr('src', img.replace(color, 'BLACK'));

}

function unhighlight_arrow(arrow) {
    var color = 'WHITE';
    if(arrow.hasClass('selected')) {
        color = 'BLUE';
    }
    arrow = arrow.children().children();
    var img = arrow.attr('src');
		if(img) {
			//see if the previous tab is selected
			arrow.hasClass('theme_tab_arrow_selected');
			arrow.attr('src', img.replace('BLACK', color));
		}
}

function attach_quick_add_submit() {
	
	$('form[name=quick_add]').unbind().bind('submit', function(e) {
			url = $(this).attr('action');
			children = $(this).children();
			type_id = children.filter('[name=type_id]').val();
			type = children.filter('[name=type]').val();

			return add_to_cart(url, type_id, type);
			
		});

}