///<reference path="/layout/js/lib/jquery.min.js" />
var GlobalFieldID = { carttotal: '#cartitems'};
var layoutDir = { slideshow: '/layout/images/slideshow/' };

function CookieJWeb() {}
CookieJWeb.prototype.SetQuickView = function () {
	if ($('#content .product-rotate .items .proditems').length > 0) {
		$('#content .product-rotate .items .proditems').hover(function () {
			$(this).children('.quick-view').css('display', 'block');
		}, function () {
			$(this).children('.quick-view').css('display', 'none');
		});
	};
}
CookieJWeb.prototype.SetSlideShow = function () {
	$(document).bgStretcher({
		images: [layoutDir.slideshow + '0101_home.jpg', layoutDir.slideshow + '0102_home.jpg', layoutDir.slideshow + '0103_home.jpg', layoutDir.slideshow + '0104_home.jpg'],
		imageWidth: 1280, imageHeight: 780, extSlideDelay: 10000, slideShowSpeed: 'slow'
	});
}
CookieJWeb.prototype.SetDropDown = function(selector, dropcont) {
	$(selector).hover(function () {
		clearTimeout($.data(this, 'timer'));
		$(dropcont, this).stop(true, true).fadeIn();
	}, function () {
		$.data(this, 'timer', setTimeout($.proxy(function () {
			$(dropcont, this).stop(true, true).fadeOut();
		}, this), 200));
	});
}
CookieJWeb.prototype.ToggleDropDown = function () {
	($.browser.msie) ? $('.popup').show() : $('.popup').fadeIn();
	setTimeout(function () {
	($.browser.msie) ? $('.popup').hide() : $('.popup').fadeOut();	}, 3200);

}
CookieJWeb.prototype.SetShopDropDown = function () {
	cj.SetDropDown('#header .main-menu UL LI', '.sub-menu');
}
CookieJWeb.prototype.LoadMiniCart = function() {
	cj.SetDropDown('#header UL.top-menu LI', '.popup');
	cj.ShowMiniCart();
}
CookieJWeb.prototype.ShowMiniCart = function () {
	var url = '/minicart.aspx';
	$.ajax({
		url: url,
		success: function (html) {
			$('#mini-cart-container').empty();
			$('#mini-cart-container').append(html);
		},
		error: function (e) {
			alert(e.responseText);
		}
	});
} 

CookieJWeb.prototype.GetCartCount = function () {
	var xmlhttp = false;
	/*@cc_on@*/
	/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end@*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	try {
		if (topdomain) {
			var prot = "http://";
			if (isSecure())
				prot = "https://";
			xmlhttp.open("GET", prot + topdomain + "/store/os/shoppingcartcost.aspx", false);
			xmlhttp.send(null);
			var str = xmlhttp.responseText;
			xmlhttp.close;
			xmlhttp = false;
			var ary = str.split("|");
			if (ary.length > 1) {
				cj.BindCartTotal(ary[1]);
			}
		}
	}
	catch (e) { }
}
CookieJWeb.prototype.BindCartTotal = function (carttotal) {
	jQuery(GlobalFieldID.carttotal).html(carttotal);
}

CookieJWeb.prototype.SetCufon = function () {
	Cufon.replace('h2')('ul#topnav', { hover: { color: '#1523E6'} })('#sub1')('.subcategory', { hover: { color: '#1523E6'} })('h2.cattitle');
}

CookieJWeb.prototype.LoadCufonSlow = function () {
	$('#content .main-content .product-details H2').css('visibility', 'visible');
	$('.product-details H2').css('visibility', 'visible');
	$('H2.cattitle').css('visibility', 'visible');
	$('h2').css('visibility', 'visible');
}

CookieJWeb.prototype.SetTabs = function () {
	$('#tabs A').click(function () {
		curr = $(this).attr('href');
		$('#tabs LI').removeClass('active');
		$(this).parent('li').addClass('active');
		$('.tab').removeClass('open');
		$(curr).addClass('open');
		return false;
	});
}

trigger_cartajax = function () {
	cj.GetCartCount();
	cj.LoadMiniCart();
	cj.ToggleDropDown();
}

var cj = new CookieJWeb();

$(function () {
	cj.SetShopDropDown(); cj.LoadMiniCart(); cj.GetCartCount(); cj.LoadCufonSlow(); cj.SetCufon(); cj.SetTabs();
});


function removeItem(recid) {
	var params = "{ recid : '" + recid + "' }";
//	$("#mincart-overlay").ajaxStart(function () { $(this).show(); alert("started"); });
//	$("#mincart-overlay").ajaxStop(function () { $(this).show(); alert("ended"); });
	$.ajax({
		type: 'POST',
		contentType: 'application/json; charset=utf-8',
		url: "/minicart.aspx/RemoveItem",
		data: params,
		success: function (str) {
			$('#minicart-message').html("<span class='alert'>Item removed from your shopping bag.</span>");
			setTimeout(function () { cj.ShowMiniCart(); cj.GetCartCount(); }, 2000);
		},
		error: function (request, status, error) {
			alert(request.responseText);
		}

	});
}

function toggleDesc() {
	var desc = document.getElementById('DescriptionHidden').value;
	var descblock = document.getElementById('descblock');
	var descblockcontainer = document.getElementById('descblockcontainer');

	if (descblockcontainer.style.display == 'none') {
		descblockcontainer.innerHTML = desc;
		descblockcontainer.style.display = 'block';
	} else {
		descblockcontainer.innerHTML = '';
		descblockcontainer.style.display = 'none';
	}
}

$(document).ready(function () {

    var pc = getElementByID_Master("ProductColors");

    if (pc != null) {

        var length = pc.options.length;

        if (length == 2)
            getElementByID_Master("ProductColors").selectedIndex = 1;

        if (aSKUPreviousCost.length > 0) {
            if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                $('#sale-price').removeClass('sale-price');
                $('#sale-text').hide();
            }
            else { $('#sale-text').show(); }

            $(".unselected").click(function () {
                if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                    $('#sale-price').removeClass('sale-price');
                    $('#sale-text').hide();
                } else {
                    $('#sale-price').addClass('sale-price');
                    $('#sale-text').add();
                }
            });

            $(".selected").click(function () {
                if (aSKUPreviousCost[getElementByID_Master("ProductColors").selectedIndex][1] == "") {
                    $('#sale-price').removeClass('sale-price');
                    $('#sale-text').hide();
                } else {
                    $('#sale-price').addClass('sale-price');
                    $('#sale-text').add();
                }
            });
        }
    }

    //----------- added for Can't Find Your Size ---------------
    $('a.popup-box').click(function () {
        var id = $(this).attr('id');
        load_cantfindyoursize(id);
        return false;
    });
});


//-------------------------- Can't Find Your Size --------------------------
function load_cantfindyoursize(id) {
    var url = '/store/product_email_signup.aspx?productid=' + id;
    $.ajax({
        url: url,
        success: function (html) {
            $('#product-popup').html(html);
            initPopup('#product-popup', '#product-popup .close-btn');
        },
        error: function (e) {
            alert(e.responseText);
        }
    });
}

function initPopup(obj, close) {
    $('.wrapper').height($(document).height());
    $('.wrapper').width($(window).width());
    $('.wrapper, ' + obj).show();
    $('#realZOOMHighlight').css('width', '0px');
    $('#realZOOMHighlight').find('div').css('width', '0px');
    $('#realZOOMImageOuter').css('width', '0px');
    $('#realZOOMImageOuter').css('borderWidth', '0px');
    $('#realZOOMBG').css('width', '0px');
    $('#realZOOMBG').css('borderWidth', '0px');
    $('#realZOOMBGText').css('display', 'none');
        $(close + ', .wrapper').click(function () {
            closeWindow();
        });
}

function OutOfStock(productid) {
    var modelname = document.getElementById('ModelName').innerHTML;
    var size = document.getElementById('ProductSizes').value;
    var color = null;
    if (document.getElementById('ProductColors') != null) {
        color = document.getElementById('ProductColors').value;
    } else if (document.getElementById('ContentPlaceHolder1_ProductColors') != null) {
        color = document.getElementById('ContentPlaceHolder1_ProductColors').value;
    } else {
        color = cantfindsizeColorHolder;
    }
    var name = document.getElementById('Name').value;
    var email = document.getElementById('EmailAddress').value;
    var newsletterchk = null;
    if (document.getElementById('NewsLetterChk') != null)
        newsletterchk = document.getElementById('NewsLetterChk').checked;
        
    if(size == undefined ||size == null || size == "")
        size = "0";
    if(color == undefined || color == null || color == "")
        color = "0";

    if (size == '-1') {
        showError('Please select a size!');
        return false;
    }

    if (color != null && color == '-1') {
        showError('Please select a color!');
        return false;
    }

    if (name == '') {
        showError('Please enter a name!');
        return false;
    }

    if (email == '') {
        showError('Please enter a email!');
        return false;
    }

    if (email != "") {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
            var url = '/store/os/cantfindyoursizesubmit.aspx';
            var params = '?productid=' + productid + '&colorid=' + color + '&sizeid=' + size + "&name=" + name + "&email=" + email + "&newsletterchk=" + newsletterchk + "&modelname=" + modelname;

            url += params;

            showError('processing...');

            $.ajax({
                url: url,
                success: function (transport) {
                    $('#message').html('Thank you. Your request has been submitted.<br>This window will close in 5 seconds.');
                    t = setTimeout(closeWindow, 6000);
                },
                error: function () {
                    $('#message').html('Your request has not been submitted!');
                }
            });

        } else {
            showError('Invalid E-mail Address! Please re-enter.');
            return false;
        }
    }
    return false;
}

function showError(err) {
    $('#message').show();
    $('#message').css("color", "#ffffff");
    $('#message').css("padding", "5px");
    $('#message').css('background-color', '#808184');
    $('#message').css('display', 'block');
    $('#message').html(err);
}

function closeWindow() {
    $('#product-popup').hide();
    $('.wrapper').hide();
    $('#realZOOMHighlight').css('width', 'auto');
    $('#realZOOMImageOuter').css('width', '410px');
    $('#realZOOMBG').css('width', 'auto');
    $('#realZOOMBGText').css('display', 'block');
}
//-------------------------- End - Can't Find Your Size --------------------------
