$(document).ready(function(){
						   
	
	// CSS to give the modal overlay its transparency
	$("head").append("<style type=\"text/css\">.ui-widget-overlay { opacity: 0.7; filter: alpha(opacity=70); }</style>");
	
	$('div#contentcontainer').pngFix();
	
	$("tr").hover(function() {
		$(this).addClass("over");
	}, function() {
		$(this).removeAttr("class");
	});
	
	
	// Search boxsearchAMIGO = $('#searchEBAmigo').val();
	searchUrbanStone = $('.sf_searchBox input[type=text]').val();
	$(".sf_searchBox input[type=text]").focus(function() {
		if($(this).val() == searchUrbanStone) {
			$(this).val("");
		}
	}).blur(function () {
		if($(this).val() == "") {
			$(this).val(searchUrbanStone);
		}
	});
	
	
	// IE6 Left Col Opacity
	if($.browser.msie) {
    	if($.browser.version < 7.0) {
			$('#contentcontainer').css("opacity", 0.8);
		}
	} 
	
	bottomPadding();
	
//	$("a.tier1").click(function() {
//		var category = $(this).parent("li");
//		if(!(category.hasClass("active"))) {
//			$(".information-tables .left-column li ul").slideUp("normal");
//			$("ul",category).slideDown();
//			$(".information-tables .left-column li").removeClass("active");
//			category.addClass("active");
//		}
//		return false;
//	});
	
});

showModalCommon=function(sHtml,oDim)
{
	if($(".ui-dialog").length>0)return;
	if(!sHtml)sHtml="<img class='loader' src='/wsimages/loader-whitebg.gif' />";
	var oPos ="center";

	var shadow = '<div class="edge-handle edge-n"></div><div class="edge-handle edge-s"></div><div class="edge-handle edge-e"></div><div class="edge-handle edge-w"></div><div class="edge-handle edge-ne"></div><div class="edge-handle edge-sw"></div><div class="edge-handle edge-se"></div><div class="edge-handle edge-nw"></div>'
	
	var mod=$("<div class='ucModal'><div class='modal_content'>"+sHtml+"</div>" + shadow + "</div>").dialog({closeText:'x', close:closeModal,modal:true, overlay: {"background-color": "#000","opacity": "0.50","-moz-opacity": "0.50"}, width:oDim.w, height: oDim.h, resizable:false});
	
}

function reCenterCommon(){
	var dialog=$(".ui-dialog");
	var win=$(window);
	dialog.css({left:(((win.width() /2)-(dialog.width()/2))) + win.scrollLeft() ,top:((win.height() /2)-(dialog.height()/2)) + win.scrollTop()});
}

function bottomPadding(){
	var hasShortlist = $("#shortlist_container li").length;
	if(hasShortlist > 0) {
		$("body").addClass("padding-shortlist");
		shortListHidden();
		$(window).resize(function() {
			shortListHidden();
		});
	}
}

function shortListHidden() {
	
	var contianerWidth = $("#quote_shortlist").width() - 249;
	var itemsWidth = $("#shortlist_container li").length * 185;
	var totalItems = $("#shortlist_container li").length;
	
	//alert(itemsWidth + " > " + contianerWidth);
	
	if(itemsWidth > contianerWidth) {
		$("#quote_shortlist .plus_more").show();
		$("#quote_shortlist .plus_more span").html(totalItems);
	} else {
		$("#quote_shortlist .plus_more").hide();
	}
	if(totalItems==0)
	{
	    $("#quote_shortlist").fadeOut();
//	    $("#shortlist_container").slideDown('fast',function(){
//	    });
	}else{
	    $("#quote_shortlist").show();
	    bottomPadding();
	}
}

function OpenTextureColourGrid(ProductID)
{
    showModalCommon("",{w:615, h:440});
    $(".modal_content").load("/UserControls/Products/ColourTextureViewer.aspx", { id: ProductID }, reCenterCommon);
	return false;
}

function openSpecialsMap()
{
    showModalCommon("",{w:560});
	$(".modal_content").load("/UserControls/ContactForm/map.aspx",{},reCenterCommon);
	return false;
}

function OpenDisclaimer(ProductID)
{
    showModalCommon("",{w:539, h:150});
	$(".modal_content").load("/UserControls/Products/DisclaimerViewer.aspx",{id:ProductID},reCenterCommon);
	return false;
}

function OpenSendEnquiry(ProductID)
{
    showModalCommon("",{w:400, h:415});
	$(".modal_content").load("/UserControls/Products/SendEnquiry.aspx",{ProductID: ProductID},reCenterCommon);
	
	return false;
}

function closeModal()
{
	$(".ucModal").remove();
}


function GetaQuote(ProductID)
{
    showModalCommon("",{w:515, h:310});
	$(".modal_content").load("/UserControls/Products/QuoteModal.aspx",{id:ProductID},reCenterCommon);
	return false;
}

function GetMap(url)
{
    showModalCommon("",{w:494, h:549});
	$(".modal_content").load("/UserControls/Products/MapModal.aspx", {mapsData:url}, reCenterCommon);
	return false;
}

function ChangeColourQuote()
{
    $(".loader").hide();
    
    $.ajax({
      type: "POST",
      url: "/UserControls/Products/QuoteModal.aspx/GetTexturesByColourID",
      data: '{"ProductID":"' + $("input[id$=ProductID]").val() + '", "ColourID":"' + $("select[id$=ColourRange]").val() + '"}',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(result) {
        result = result.d;
        populateSelect(document.getElementById($('select[id$=TextureRange]').attr("id")), result);
      }
    });
}

function populateSelect(el, items) 
{
    el.options.length = 0;
    if (items.length > 0)
        el.options[0] = new Option('Please select', '');

    $.each(items, function () {
        el.options[el.options.length] = new Option(this.Name, this.ID);
    });
}

function RemoveProduct(ItemID)
{
    $.ajax({
      type: "POST",
      url: "/UserControls/Products/QuoteModal.aspx/RemoveProduct",
      data: '{"ItemID":"' + ItemID  + '"} ',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(result) {
        result = result.d;
        if(result == true)
        {
            $(".Product"+ItemID).fadeOut("fast", function(){
                $(".Product"+ItemID).remove();
                shortListHidden();
            });
        }
      }
    });
    return false;
}
function AddProductToCart()
{
    //Validate form items first
    var ProductID = $("input[id$=ProductID]").val();
    var AreaSize = $("input[id$=f_areaSize]").val();
    var ColourID = $("select[id$=ColourRange]").val();
    var TextureID = $("select[id$=TextureRange]").val();

    var ColumnValues = "";
    
    AreaSize = parseFloat(AreaSize);
    if(isNaN(AreaSize))
    {
        alert('Please enter a number into the area size field');
        return false;
    }
    
    if(TextureID == "")
    {
        alert('Please select a Texture');
        return false;
    }

    $("#get_quote").hide();
    $(".loader").show();

    $('div.selectList div.col > select').each(function(index) {
        ColumnValues+= $(this).val() + ",";
    });
    
    if(ColumnValues.length>0)
        ColumnValues = ColumnValues.substring(0,ColumnValues.length - 1); 
    
    $.ajax({
      type: "POST",
      url: "/UserControls/Products/QuoteModal.aspx/AddProduct",
      data: '{"ProductID":"' + ProductID  + '", "AreaSize":' + AreaSize + ', "ColourID":"' + ColourID + '", "TextureID":"' + TextureID + '", "ColumnValues":"' + ColumnValues + '"} ',
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(result) {
        result = result.d;
        if(result != "")
        {
            closeModal();
            if($("#shortlist_container li").length==0)
            {
                $("#shortlist_container").append("<ul></ul>");
            }
            $("#shortlist_container > ul").append(result);
            shortListHidden();            
        }
      }
    });
    return false;
}

function popWindow(url) {
	var winH = 660;
	var winW = 1000;
	
	if($.browser.msie) { 
		var winH = 660;
	} 
	if($.browser.safari) { 
		var winH = 660;
	}
	
	window.open(url,"popUpWindow",'height='+winH+',width='+winW+',status=yes,toolbar=no,menubar=no,location=no,scrolling=no');
}


