/* Setup namespaces */
if (typeof(BCNTRY) == 'undefined') {BCNTRY = {};}
if (typeof(BCNTRY.swatches) == 'undefined') {BCNTRY.swatches = {};}
if (typeof(BCNTRY.pdp) == 'undefined') {BCNTRY.pdp = {};}


function AddSwatchToURL(thislink, imageName) {
	var imageCGIName = 'swatch'; //The code on the other end of this URL expects this variable
	var imageSrc = '';

	if (imageName == '') {
		//Default imagename is 'mainimage'
		imageSrc = document.images['mainimage'].src;
	} else {
		imageSrc = document.images[imageName].src;
	}

	//Get just the filename of the image (with no path)
	var filename = imageSrc.substring(imageSrc.lastIndexOf('/') + 1, imageSrc.lastIndexOf('.'));

	//Figure out how to append image swatch based on possible presence of query string
	var tmpURL = thislink.href;

	if (-1 == tmpURL.indexOf('?')) {
		//No querystring on the URL, so we create our own
		tmpURL = tmpURL + '?';
	} else {
		//Append another name/value pair to existing querystring
		tmpURL = tmpURL + '&';
	}

	tmpURL = tmpURL + imageCGIName + '=' + filename;

	//Send it
	window.location = tmpURL;
	return false;
}



function ItemImagePicker(newSelIndex, newImageURL) {

	//Swap the image
	document.mainimage.src = newImageURL;

	//Reset both select widgets IF they exist.
	if (document.flypageside && document.flypageside.mv_sku) {
		document.flypageside.mv_sku.selectedIndex 	= newSelIndex;
	}

	if (document.flypage && document.flypage.mv_sku) {
		document.flypage.mv_sku.selectedIndex		= newSelIndex;
	}

	return false;
}


function wopen(url, w, h)
{
	w += 32;
    h += 96;
	var win = window.open(url,
		'popup', 
		'width=' + w + ', height=' + h + ', ' +
		'location=no, menubar=no, ' +
		'status=no, toolbar=no, scrollbars=yes, resizable=no');
	win.resizeTo(w, h);
	win.focus();
}

function freeshippop(){
	var popurl="/"+catalog+"/free_shipping.html";
	winpops=window.open(popurl,"","width=600,height=175,scrollbars,resizable,toolbar,")
}

var opt = 0;
function matchopts() {
//Sync all the "Select Color/Size select widgets when the user
//changes one of them.
	if(document.flypageside.mv_sku.selectedIndex != opt) {
		opt = document.flypageside.mv_sku.selectedIndex;
		document.flypage.mv_sku.selectedIndex = opt;
	}
	else if(document.flypage.mv_sku.selectedIndex != opt) {
		opt = document.flypage.mv_sku.selectedIndex;
		document.flypageside.mv_sku.selectedIndex = opt;
	}
}

function checkoptions(form) {
	if(form.mv_sku) {
		if(form.mv_sku.length > 1) {
			if(form.mv_sku.selectedIndex == 0) {
				 BCNTRY.pdp.none_selected_popup_show();
                if (typeof(ScVariantPop) !== 'undefined') {
                    var scv = new ScVariantPop();
                }

				return false;
			}
		}
	}
	return true;
}

BCNTRY.pdp.none_selected_popup = new YAHOO.widget.Panel("none_selected_popup",
{
    close:true,
    visible:false,
    draggable:true,
    modal:true,
    zIndex:500,
    fixedcenter:true
});

BCNTRY.pdp.updateSubmitPanel = function (variant_id) {
    var variant = $(variant_id);
    BCNTRY.pdp_images.set_from_dropdown(variant.id);
    variant.selected = true;
    BCNTRY.pdp.none_selected_popup_hide();
    var form = document.flypage;
    form.submit();
};

BCNTRY.pdp.none_selected_popup_hide = function () {
   $('none_selected_popup').style.display = 'none';
   $('select_size').style.visibility = 'visible';
   BCNTRY.pdp.none_selected_popup.hide();
};

BCNTRY.pdp.none_selected_popup_show = function () {
   $('none_selected_popup').style.display = 'block';
   // hide the dropdown bar
   $('select_size').style.visibility = 'hidden';
   BCNTRY.pdp.none_selected_popup.show();
   var none_selected_popup_mask = $('none_selected_popup_mask');
   if (none_selected_popup_mask !== null) {
       none_selected_popup_mask.onclick = BCNTRY.pdp.none_selected_popup_hide;
       if (none_selected_popup_mask.style.zIndex >= 500) {
           none_selected_popup_mask.style.zIndex = 499;
       }
   }
};

ye.addListener( 'none_selected_popup_mask', 'click', BCNTRY.pdp.non_selected_popup_hide );
BCNTRY.pdp.none_selected_popup.render();

// to do once flypage is loaded
function page_init() {
}

// run page_init function on page load finish
ye.addListener(window, 'load', page_init);
