$(document).ready(function(e) {
	
	

    //First we check for cart totals
    if(current_page != "checkout"){
        check_cart_totals();
    }

    $("#my_cart").dialog({
        show:'blind',
        hide:'blind',
        autoOpen: false,
		draggable: false,
		position: ['center',184],
		resizable:false,
        width:  897,
        height: 427,
        'zIndex':1001,
        dialogClass: 'the_cart',
        title: 'Your Shopping Bag',
        close: function(event, ui){
            block_all_ui("Just a moment...", 380, 0);
            location.reload();
        }

    });
    $("#view_cart").click(function(){
        block_all_ui("Please wait while we setup your shopping bag", 580, 0);
        refresh_cart();
    });

    $(".buy_now,.buy_now_common").click(function(){


        //$("#choose_size_"+$("#chosen_size_index").val()).css("border", "1px solid white");
        $("#choose_size_"+$("#chosen_size_index").val()).fadeTo("slow", 1);


        //Make ajax call to add this item to the Smart-Cart
        if($("#chosen_size").val()){
            block_all_ui("Please wait while we setup your shopping bag", 580, 0);

            $.post(

                    BASEURL + "ajax/add_to_cart",
                    {
                        'product_id': $("#chosen_product_id").val(),
                        'size': $("#chosen_size").val(),
                        'color': $("#chosen_color").val(),
                        'qty': $("#chosen_size_qty").val(),
                        'update_qty': $("#update_cart").val()
                    },

                    //This is where data has been returned.
                    function(data){
                        var remaining_size_qty = parseInt(data.remaining_size_qty);
                        var remaining_product_qty = parseInt(data.real_product_qty);
                        var cmp_against = parseInt(remaining_size_qty +1);

                        if(cmp_against > 0){
                            //Open the dialog once we have all data from the ajax call
                            refresh_cart();
                        }
                        if($("#location").val() == "product"){
                            //Make product sold out
                            if(remaining_product_qty <= 0){
                                setTimeout('make_product_sold_out('+$("#chosen_product_id").val()+')',500);
                            }else{
                                setTimeout('not_make_product_sold_out('+$("#chosen_product_id").val()+')',500);
                            }

                            if(remaining_size_qty == 0){
                                setTimeout('make_size_sold_out()',500);
                            }else{
                                setTimeout('not_make_size_sold_out()',500);
                            }
                            $("#chosen_size").val("");
                        }

                        update_header_text(data);


                        if(data.Response == "Insufficient Stock"){
                            //We update the size styles according to the new size quantity
                            status_bar("WARNING: Insufficient stock. Please select another size or less quantity.");
                        }
                        $.unblockUI();
                    },
                    "json"
            );

        }else{
            status_bar("IMPORTANT: Please select a size first");
        }
    });
	
	//set the nice tooltip for the top header alerts
	$("#top_login_email_label").tooltip();
	$("#top_login_password_label").tooltip();

});


function check_cart_totals(){

    $.get(BASEURL + "ajax/get_cart_totals",{},
        function(data){
            update_header_text(data);
        },
        "json"
    );
}


function update_header_text(data){
    var total_in_cart = data.total_in_cart;
    if(total_in_cart > 0){
        $("#total_cart_items").html("THERE ARE "+total_in_cart+" ITEMS IN YOUR BAG");
     }else{
         $("#total_cart_items").html("YOU HAVE NO SELECTED ITEMS");
     }
     if(total_in_cart == 1){
         $("#total_cart_items").html("THERE IS "+total_in_cart+" ITEM IN YOUR BAG");
     }
     return total_in_cart;
}




function refresh_cart(){

    $.post(

        BASEURL + "ajax/show_cart",{},
        function(data){
            $("#my_cart").html(data);
            $("#my_cart").dialog("open");
            $.unblockUI();
        },
        "html"
    );
}

function validateEmail(elementValue){
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
}

function old_validate_top_login(){

    $("#top_login_email").css("border","1px solid #3D3D3D");
    $("#top_login_password").css("border","1px solid #3D3D3D");
    var err = 0;
    if(!validateEmail($("#top_login_email").val())) {
        $("#top_login_email").css("border","1px solid white");
        err = 1;
    }

    if($("#top_login_password").val() == '') {
        $("#top_login_password").css("border","1px solid white");
        err += 2;
    }

    switch(err)
    {
        case 1:
            status_bar("Please enter a valid email");
            $("#top_login_password").css("border","1px solid #3D3D3D");
            break;
        case 2:
            status_bar("Please enter a password");
            $("#top_login_email").css("border","1px solid #3D3D3D");
            break;
        case 3:
            status_bar("Please enter a valid email and a password");
            break;
        default:
            $("#top_login_email").css("border","1px solid #3D3D3D");
            $("#top_login_password").css("border","1px solid #3D3D3D");
            $("#top_login").submit();
    }
}

function validate_top_login(){
	var valid = true;
	$(".exclamation_label_header1").css("visibility", "hidden");
	$(".exclamation_label_header2").css("visibility", "hidden");
	
	
	var email = $("#top_login_email").val();
	
	if(!valid_email(email)) {
		$("#top_login_email_label").css("visibility", "visible");
		valid = false;
	}
	
	var password = $("#top_login_password").val();
	if(password.length < 1) {
		$("#top_login_password_label").css("visibility", "visible");
		valid = false;
	}
	return valid;
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		//limitCount.value = limitNum - limitField.value.length;
	}
}


