function initPopups()
{
	initPopup({
		openEvent:'click'
	});
	initPopup({
		popupHolderClass:'popup-hover'
	});
}
if (window.addEventListener)
	window.addEventListener("load", initPopups, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPopups);
	
function initPopup(_popup) {
	if (!_popup.popupHolderTag) _popup.popupHolderTag = 'li';
	if (!_popup.popupTag) _popup.popupTag = 'div';
	if (!_popup.popupHolderClass) _popup.popupHolderClass = 'popup-holder';
	if (!_popup.popupClass) _popup.popupClass = 'popup';
	if (!_popup.linkOpenClass) _popup.linkOpenClass = 'open';
	if (!_popup.linkCloseClass) _popup.linkCloseClass = 'close';
	if (!_popup.openClass) _popup.openClass = 'active';
	if (!_popup.openEvent) _popup.openEvent = 'hover';
	
	var timer = [];	
	var _popupHolderTag = document.getElementsByTagName(_popup.popupHolderTag);
	if (_popupHolderTag) {
		for (var i=0; i<_popupHolderTag.length; i++) {
			if (_popupHolderTag[i].className.indexOf(_popup.popupHolderClass) != -1) {
				var _popupLink = _popupHolderTag[i].getElementsByTagName('a');
				for (var j=0; j<_popupLink.length; j++) {
					_popupLink[j].parent = _popupHolderTag[i];
					if (_popupLink[j].className.indexOf(_popup.linkOpenClass) != -1) {
						if (_popup.openEvent == 'click') {
							_popupLink[j].onclick = function(){
								if (this.parent.className.indexOf(_popup.openClass) != -1) {
									this.parent.className = this.parent.className.replace(_popup.openClass,'');
								} else {
									this.parent.className += ' '+_popup.openClass;
								}
								return false;
							}
						} else {
							var _popupTag = _popupHolderTag[i].getElementsByTagName(_popup.popupTag);
							for (var k=0; k<_popupTag.length; k++) {
								if (_popupTag[k].className.indexOf(_popup.popupClass) != -1) {
									_popupTag[k].parent = _popupHolderTag[i];
									_popupTag[k].onmouseover = function(){
										if (timer[j]) clearTimeout(timer[j]);
										if (this.parent.className.indexOf(_popup.openClass) == -1) {
											this.parent.className += ' '+_popup.openClass;
										}
									}
									_popupTag[k].onmouseout = function(){
										var _this = this;
										timer[j] = setTimeout(function(){
											_this.parent.className = _this.parent.className.replace(_popup.openClass,'');
										},2);
									}	
								}
							}
							_popupLink[j].onmouseover = function(){
								if (timer[j]) clearTimeout(timer[j]);
								if (this.parent.className.indexOf(_popup.openClass) == -1) {
									this.parent.className += ' '+_popup.openClass;
								}
							}
							_popupLink[j].onmouseout = function(){
								var _this = this;
								timer[j] = setTimeout(function(){
									_this.parent.className = _this.parent.className.replace(_popup.openClass,'');
								},2);
							}
						}
					} else if (_popupLink[j].className.indexOf(_popup.linkCloseClass) != -1) {
						_popupLink[j].onclick = function(){
							if (this.parent.className.indexOf(_popup.openClass) != -1) {
								this.parent.className = this.parent.className.replace(_popup.openClass,'');
							} else {
								this.parent.className += ' '+_popup.openClass;
							}
							return false;
						}
					}
				}
			}		
		}
	}
}

function popup_size_chart(w) {
    var pw;
    pw = window.open(w, 'SizeChart', 'height=450,width=720,toolbar=no,resizable=no,scrollbars=yes');
    pw.focus();
}

J$(document).ready(function () {
    J$('.thumbnails li a').each(function (index) {
        if (J$(this).attr('onmouseover') != null && J$(this).attr('onmouseover').toString().toLowerCase().indexOf('shadow') > -1) {
            J$(this).children().remove();
            J$(this).removeAttr('onmouseover').removeAttr('onmouseout').removeAttr('href');
        }

    });

    if (J$('#ContentPlaceHolder1_CollectionView_CollectionViewHL').length > 0) {
        J$('#ContentPlaceHolder1_StreetStyle1_StreetStyleHL img').css('padding-top', '10px');
    }

    //----------- added for (document).ready Can't Find Your Size ---------------
    J$('.jquery-cfys-popup').click(function () {
        var id = J$(this).attr('id');
        load_cantfindyoursize(id);
        return false;
    });
    //add divs to end of body to eliminate embedded position:relatives
    J$('body').append('<div class="darkwrapper">&nbsp;</div>');
    J$('body').append('<div id="product-popup"></div>');
    //----------- End (document).ready - Can't Find Your Size ------------------
});


//----------------- added for Can't Find Your Size --------------------------
function load_cantfindyoursize(id) {
    var url = '/store/product_email_signup.aspx?productid=' + id;
    J$.ajax({
        url: url,
        success: function (html) {
            J$('#product-popup').html(html);
            initCyfsPopup('#product-popup', '#product-popup .close-btn');
        },
        error: function (e) {
            alert(e.responseText);
        }
    });
}

function initCyfsPopup(obj, close) {
    J$('.darkwrapper').height(J$(document).height());
    J$('.darkwrapper').width(J$(window).width());
    J$('.darkwrapper, ' + obj).show();
    //prevent real-zoom from popping up when mouse is in modal but over prod picture
    J$('#realZOOMHighlight').css('width', '0px');
    J$('#realZOOMHighlight').find('div').css('width', '0px');
    J$('#realZOOMImageOuter').css('width', '0px');
    J$('#realZOOMImageOuter').css('borderWidth', '0px');
    J$('#realZOOMBG').css('width', '0px');
    J$('#realZOOMBG').css('borderWidth', '0px');
    J$('#realZOOMBGText').css('display', 'none');
    J$(close + ', .darkwrapper').click(function () {
        closeCyfsWindow();
    });
}

function CfysSubmit(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') {
        showCyfsError('Please select a size!');
        return false;
    }

    if (color != null && color == '-1') {
        showCyfsError('Please select a color!');
        return false;
    }

    if (name == '') {
        showCyfsError('Please enter a name!');
        return false;
    }

    if (email == '') {
        showCyfsError('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;

            showCyfsError('processing...');

            J$.ajax({
                url: url,
                success: function (transport) {
                    J$('#message').html('Thank you. Your request has been submitted.<br>This window will close in 5 seconds.');
                    t = setTimeout(closeCyfsWindow, 6000);
                },
                error: function () {
                    J$('#message').html('Your request has not been submitted!');
                }
            });

        } else {
            showCyfsError('Invalid E-mail Address! Please re-enter.');
            return false;
        }
    }
    return false;
}

function showCyfsError(err) {
    J$('#message').show();
    J$('#message').css("color", "#ffffff");
    J$('#message').css("padding", "5px");
    J$('#message').css('background-color', '#808184');
    J$('#message').css('display', 'block');
    J$('#message').html(err);
}

function closeCyfsWindow() {
    J$('#product-popup').hide();
    J$('.darkwrapper').hide();
    //restore real zoom
    J$('#realZOOMHighlight').css('width', 'auto');
    J$('#realZOOMImageOuter').css('width', '410px');
    J$('#realZOOMBG').css('width', 'auto');
    J$('#realZOOMBGText').css('display', 'block');
}
//-------------------------- End - Can't Find Your Size --------------------------
