$(document).ready(function() { 

    $('#loyalty_code_entry fieldset input').focus(function(){
        $('#loyalty_code_entry fieldset label').hide();
    });

    $('#loyalty_code_entry fieldset label').focus(function(){
        $('#loyalty_code_entry fieldset label').hide();
    });

    $('#loyalty_code_entry fieldset input').blur(function(){
        if($(this).val() == ''){
            $('#loyalty_code_entry fieldset label').show();
        }
    });
	
    
    // activate the modal overlay
    
    $('#modal').jqm({ ajax: '@href',
                      trigger: 'a.modal_trigger',
                      target: '#remote_content' 
    });
    
    // remove the last border from the global nav
    $("#global_nav li:last a").css("border","none");
    
    // replace the no flash button with a text link
    
    var noflashCopy = $("#noflash_link input[@type='submit']").val()
    $("#noflash_copy").append(noflashCopy);
    $("#noflash_copy").click(function(){
        $("#noflash_link").submit();  
    });
    $("#noflash_copy").css({cursor: "pointer",
                            textDecoration: "underline"});
    $("#noflash_link").hide();
                      
    // generate popups for global nav items
                      
    $("a[@rel=popup]").click(function(){
           popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=500px,height=600,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
           popupWindow.focus()
           return false;
    })

    $("a[@rel=external]").click(function(){
           popupWindow = window.open(this.href,this.title,'width=800,height=600,toolbar=yes,status=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
           popupWindow.focus()
           return false;
    })
    
    // submit user's flash version with the FAQ form
    
    var flash = deconcept.SWFObjectUtil.getPlayerVersion();
    $("input#flash").val(flash.major + '.' + flash.minor + '.' + flash.rev);
    
    // Popup window close button
    
    $("#popup_close_button").css("cursor","pointer");
    
    $("#popup_close_button").click(function() {
        window.close();
    });
	

// CATALOG

	$('select[@name="cat_id"]').change(function()	{
		var current_catalog = $('select[@name="cat_id"]').val(); //returns value (number) of selected option in list
		var catalog_text = $('option[@value=' + current_catalog + ']').text(); //gets the matching text value of that number
		var catalog_input = $('input[@id="catalog_name"]'); //sets the value equal to matching text
		
		if(current_catalog == '')	{ //if current catalog is blank (select default)
			catalog_input.val(''); //set the input value to be blank, do not show 'Select'
		} else { 
			catalog_input.val(catalog_text); //else, populate the input with the text of the current option
		}
	});
	
	$('.magazine_popup_activator').click(function() {
		var magazine_name = $(this).find('p.catalog_name').text();
		var magazine_description = $(this).find('p.catalog_description').text();
		var magazine_id = $(this).find('p.catalog_id').text();
		var magazine_img = $(this).find('p.catalog_img').text();
		
			$('.magazine_info h2').empty(); 
			$('.magazine_info p').empty();	
			
			$('.magazine_info h2').append(magazine_name);
			$('.magazine_info p').append(magazine_description);	
			$('.magazine_info a').attr('name', magazine_id);
			$('.magazine_info a').attr('id','magazine_link' + magazine_id);
			$('#magazine_cover img').attr('src', magazine_img);
		
			$('.magazine_info').fadeIn('slow');
	});
	
	$('a.magazine_link').click(function()	{ //when link is clicked
		var magazine_link = $(this).attr("name"); //grab the link name which is cat_id
		
		var current_catalog = $('select[@name="cat_id"]').val(magazine_link); //set the dropdown menu to reflect the cat_id
		
		var catalog_text = $('option[@value=' + magazine_link + ']').text(); //gets cat_id value and matches it to the text in options
		var catalog_input = $('input[@id="catalog_name"]'); //finds correct input box to insert text
			catalog_input.val(catalog_text); //inserts text
		
		$('.magazine_info').fadeOut('slow');
	});
	
	$('a[@name="close"]').click(function() {
		$('.magazine_info').fadeOut();
	});
	
	
//customz buttons - give a gift affiliate
	 
	/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
	 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
	 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
	 * 
	 * Version 2.1
	 * 
	 * Thanks to 
	 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
	 * Tom Leonard for some improvements
	 * 
	 */
	jQuery.fn.extend({
	/**
	* Returns get parameters.
	*
	* If the desired param does not exist, null will be returned
	*
	* To get the document params:
	* @example value = $(document).getUrlParam("paramName");
	* 
	* To get the params of a html-attribut (uses src attribute)
	* @example value = $('#imgLink').getUrlParam("paramName");
	*/ 
	 getUrlParam: function(strParamName){
		  strParamName = escape(unescape(strParamName));
		  
		  var returnVal = new Array();
		  var qString = null;
		  
		  if ($(this).attr("nodeName")=="#document") {
			//document-handler
			
			if (window.location.search.search(strParamName) > -1 ){
				
				qString = window.location.search.substr(1,window.location.search.length).split("&");
			}
				
		  } else if ($(this).attr("src")!="undefined") {
			
			var strHref = $(this).attr("src")
			if ( strHref.indexOf("?") > -1 ){
				var strQueryString = strHref.substr(strHref.indexOf("?")+1);
				qString = strQueryString.split("&");
			}
		  } else if ($(this).attr("href")!="undefined") {
			
			var strHref = $(this).attr("href")
			if ( strHref.indexOf("?") > -1 ){
				var strQueryString = strHref.substr(strHref.indexOf("?")+1);
				qString = strQueryString.split("&");
			}
		  } else {
			return null;
		  }
			
		  
		  if (qString==null) return null;
		  
		  
		  for (var i=0;i<qString.length; i++){
				if (escape(unescape(qString[i].split("=")[0])) == strParamName){
					returnVal.push(qString[i].split("=")[1]);
				}
				
		  }
		  
		  
		  if (returnVal.length==0) return null;
		  else if (returnVal.length==1) return returnVal[0];
		  else return returnVal;
		}
	});
	
	var param1 = $(document).getUrlParam("affiliate_id"); 
	//var newParam1 = param1.replace(/_/g, "&nbsp;");
	if(param1 != null)	{
		var key_link = $('.buttons a[@href="https://m1.buysub.com/webapp/wcs/stores/servlet/HomePageView?langId=-1&storeId=15801&catalogId=17303"]');
			var key_value = 'https://m1.buysub.com/webapp/wcs/stores/servlet/HomePageView?langId=-1&storeId=15801&catalogId=17303&sourcekey=';	
			key_link.attr('href',key_value + param1);
	}
});      


/*
Copyright (c) Copyright (c) 2007, Carl S. Yestrau All rights reserved.
Code licensed under the BSD License: http://www.featureblend.com/license.txt
Version: 1.0.4
*/
var FlashDetect = new function(){
    var self = this;
    self.installed = false;
    self.raw = "";
    self.major = -1;
    self.minor = -1;
    self.revision = -1;
    self.revisionStr = "";
    var activeXDetectRules = [
        {
            "name":"ShockwaveFlash.ShockwaveFlash.7",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash.6",
            "version":function(obj){
                var version = "6,0,21";
                try{
                    obj.AllowScriptAccess = "always";
                    version = getActiveXVersion(obj);
                }catch(err){}
                return version;
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        }
    ];
    /**
     * Extract the ActiveX version of the plugin.
     * 
     * @param {Object} The flash ActiveX object.
     * @type String
     */
    var getActiveXVersion = function(activeXObj){
        var version = -1;
        try{
            version = activeXObj.GetVariable("$version");
        }catch(err){}
        return version;
    };
    /**
     * Try and retrieve an ActiveX object having a specified name.
     * 
     * @param {String} name The ActiveX object name lookup.
     * @return One of ActiveX object or a simple object having an attribute of activeXError with a value of true.
     * @type Object
     */
    var getActiveXObject = function(name){
        var obj = -1;
        try{
            obj = new ActiveXObject(name);
        }catch(err){
            obj = {activeXError:true};
        }
        return obj;
    };
    /**
     * Parse an ActiveX $version string into an object.
     * 
     * @param {String} str The ActiveX Object GetVariable($version) return value. 
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseActiveXVersion = function(str){
        var versionArray = str.split(",");//replace with regex
        return {
            "raw":str,
            "major":parseInt(versionArray[0].split(" ")[1], 10),
            "minor":parseInt(versionArray[1], 10),
            "revision":parseInt(versionArray[2], 10),
            "revisionStr":versionArray[2]
        };
    };
    /**
     * Parse a standard enabledPlugin.description into an object.
     * 
     * @param {String} str The enabledPlugin.description value.
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseStandardVersion = function(str){
        var descParts = str.split(/ +/);
        var majorMinor = descParts[2].split(/\./);
        var revisionStr = descParts[3];
        return {
            "raw":str,
            "major":parseInt(majorMinor[0], 10),
            "minor":parseInt(majorMinor[1], 10), 
            "revisionStr":revisionStr,
            "revision":parseRevisionStrToInt(revisionStr)
        };
    };
    /**
     * Parse the plugin revision string into an integer.
     * 
     * @param {String} The revision in string format.
     * @type Number
     */
    var parseRevisionStrToInt = function(str){
        return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision;
    };
    /**
     * Is the major version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required major version.
     * @type Boolean
     */
    self.majorAtLeast = function(version){
        return self.major >= version;
    };
    /**
     * Is the minor version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required minor version.
     * @type Boolean
     */
    self.minorAtLeast = function(version){
        return self.minor >= version;
    };
    /**
     * Is the revision version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required revision version.
     * @type Boolean
     */
    self.revisionAtLeast = function(version){
        return self.revision >= version;
    };
    /**
     * Is the version greater than or equal to a specified major, minor and revision.
     * 
     * @param {Number} major The minimum required major version.
     * @param {Number} (Optional) minor The minimum required minor version.
     * @param {Number} (Optional) revision The minimum required revision version.
     * @type Boolean
     */
    self.versionAtLeast = function(major){
        var properties = [self.major, self.minor, self.revision];
        var len = Math.min(properties.length, arguments.length);
        for(i=0; i<len; i++){
            if(properties[i]>=arguments[i]){
                if(i+1<len && properties[i]==arguments[i]){
                    continue;
                }else{
                    return true;
                }
            }else{
                return false;
            }
        }
    };
    /**
     * Constructor, sets raw, major, minor, revisionStr, revision and installed public properties.
     */
    self.FlashDetect = function(){
        if(navigator.plugins && navigator.plugins.length>0){
            var type = 'application/x-shockwave-flash';
            var mimeTypes = navigator.mimeTypes;
            if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
                var version = mimeTypes[type].enabledPlugin.description;
                var versionObj = parseStandardVersion(version);
                self.raw = versionObj.raw;
                self.major = versionObj.major;
                self.minor = versionObj.minor; 
                self.revisionStr = versionObj.revisionStr;
                self.revision = versionObj.revision;
                self.installed = true;
            }
        }else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
            var version = -1;
            for(var i=0; i<activeXDetectRules.length && version==-1; i++){
                var obj = getActiveXObject(activeXDetectRules[i].name);
                if(!obj.activeXError){
                    self.installed = true;
                    version = activeXDetectRules[i].version(obj);
                    if(version!=-1){
                        var versionObj = parseActiveXVersion(version);
                        self.raw = versionObj.raw;
                        self.major = versionObj.major;
                        self.minor = versionObj.minor; 
                        self.revision = versionObj.revision;
                        self.revisionStr = versionObj.revisionStr;
                    }
                }
            }
        }
    }();
};
FlashDetect.JS_RELEASE = "1.0.4"; 
 
