// avoid prototype.js collisions
$jq = jQuery.noConflict();

/* Site Metrics functions (google analytics) and tracking and linking functions */
var trackerId = "UA-4338456-5"; 

// variable configurations
var animspeed = 'normal';
var txt_search = 'Enter Keywords';
var uri_imgpath = '/layouts/Cardiac Pro/img/';
var d = new Date; // used for assigning dynamic IDs to page elements

// determine and assign correct key to use for flowplayer
var fp_keys = { // all registered keys
	"sjm.com"				:	"@7b282e03bf81ce8616a",
	"*.sjm.com"				:	"#@7b282e03bf81ce8616a",
	"sjmprofessional.com"	:	"@f9b898b905329866fc7",
	"*.sjmprofessional.com"	:	"#@f9b898b905329866fc7"
};
var fp_key = '';
var hostkey = '';
var hostname = location.host;
var arr_mask = hostname.split('.');
if(arr_mask.length > 2){
	hostkey = '*.';
	hostkey += arr_mask[arr_mask.length-2]+'.';
	hostkey += arr_mask[arr_mask.length-1];
}else{
	hostkey = arr_mask[0]+'.'+arr_mask[1];
}
fp_key = fp_keys[hostkey];


// utility function for calling from inside popup iframes
// use self.parent.closeOverlay() inside an iframe to close 
// the overlay structure
function closeOverlay(){
	$jq('#js-content').trigger('click');
}
// SWFobject application for home page
function homepageFlash(){
	var flashvars = {
		xmlPathUS:flashXML_US,
		xmlPathIntl:flashXML_INTL
	};
	var params = {
		menu: "false",
		wmode:"transparent"
	};
	var attributes = {
		id: "hp-flash-movie",
		name: "hp-flash-movie"
	};
	swfobject.embedSWF("/layouts/Cardiac Pro/swf/Homepage.swf","hpfm","940","275","9.0.0","/layouts/Cardiac Pro/swf/expressInstall.swf",flashvars,params,attributes);
}

$jq(function() {
    // product image switcher
    $jq('div.img-slides .img-thumb li a').append('<span><\/span>');
    $jq('div.img-slides .img-thumb li a').bind(
		'click',
		function(x) {
		    var showNew = $jq(this).attr('class');
		    var local = $jq(this).parents('div.img-slides');
		    $jq('.active', local).removeClass('active');
		    $jq(this).parents('li').addClass('active');
		    $jq('div.img-main li:visible').fadeOut(
				animspeed,
				function() {
				    $jq('#' + showNew).fadeIn(animspeed);
				}
			);
		    return false;
		}
	);

	// Expand and collapse functions moved to tabs.js
    
    // page tool widgets
    $jq('#printpage').bind(
		'click',
		function() {
		    window.print();
		    return false;
		}
	);
    $jq('#widgets a').bind(
		'focus',
		function() {
		    this.blur();
		}
	);
    // search form
    $jq('input.search-txt').val(txt_search);
    $jq('input.search-txt').bind(
		'focus',
		function(e) {
		    if ($jq(this).val() == txt_search) {
		        $jq(this).val('');
		    }
		}
	);
    $jq('input.search-txt').bind(
		'blur',
		function(e) {
		    if ($jq(this).val() == '') {
		        $jq(this).val(txt_search);
		    }
		}
	);
    // popup displays
    $jq('body').append('<div id="js-overlay"><\/div><div id="js-content"></\div>');
    $jq('#js-content').livequery(
		'click',
		function() {
		    $jq('#js-content').fadeOut(animspeed);
		    $jq('#js-overlay').fadeOut(
				animspeed,
				function() {
				    $jq('html').removeClass('overlay-active');
				    /*
				    to get things to work in ie6 we need to completely
				    destroy and rebuild these elements
				    */
				    $jq('div#js-overlay').remove();
				    $jq('div#js-content').remove();
				    $jq('body').append('<div id="js-overlay"><\/div><div id="js-content"></\div>')
				}
			);
		}
	);
    $jq('#js-content table.overlay-content').livequery(
		'click',
		function(e) { e.stopPropagation(); }
	);
    // popup failsafe
    $jq('*[class^="pop-"]:not("a")').each(function() {
        var thetype = $jq(this).attr('class');
        $jq(this).parents('a').addClass(thetype);
    });
    // popup: video
    $jq('a.pop-video').livequery(
		'click',
		function() {
		    var theRandomNumber = d.getTime();
		    var theURL = $jq(this).attr('href');
		    $jq('#js-content').append(
				'<table class="overlay-wrapper overlay-wrapper-close" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
						'<a class="popup-video" id="vid' + theRandomNumber + '" href="' + theURL + '"></a>' +
					'<\/td><\/tr><\/table>' +
				'<\/td><\/tr><\/table>'
			);
		    $jq('#vid' + theRandomNumber).flowplayer(
				'/layouts/SJMSites/_swf/flowplayer.commercial-3.1.1.swf',
				{
				    clip: {
				        onStart: function(clip) {
				            $jq(this.getParent()).css(
								{
								    width: clip.metaData.width,
								    height: clip.metaData.height
								}
							);
				        } // more config vars can go here
				    },
				    key: fp_key
				}
			);
		    $jq('html').addClass('overlay-active');
		    $jq('#js-overlay').fadeIn(animspeed);
		    $jq('#js-content').fadeIn(animspeed);
		    return false;
		}
	);
    // popup: image
    $jq('a.pop-image').livequery(
		'click',
		function() {
		    var theURL = $jq(this).attr('href');
		    $jq('#js-content').append(
				'<table class="overlay-wrapper" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
					'<\/td><\/tr><\/table>' +
				'<\/td><\/tr><\/table>'
			);
		    $jq('table.overlay-content td:first').load(
				theURL,
				function() {
				    $jq('html').addClass('overlay-active');
				    $jq('#js-overlay').fadeIn(animspeed);
				    $jq('#js-content').fadeIn(animspeed);
				}
			);
		    return false;
		}
	);

    // popup: iframe/form
    $jq('#signup a').addClass('pop-iframe');
    $jq('a.pop-iframe').livequery(
		'click',
		function() {
		    var showClose = '';
		    var theURL = $jq(this).attr('href');
		    // Adding an option to pass the iframe window size to handle cases such as the Premere product
		    // Syntax:  <a href="" class="pop-iframe" windowX="XXX" windowY="YYY">description</a>
		    var theWidth = $jq(this).attr('windowX');
		    if (theWidth == undefined) {
		        // default width
		        theWidth = 710;
		    }
		    else {
		        showClose = ' overlay-wrapper-close ';
		    }
		    var theHeight = $jq(this).attr('windowY');
		    if (theHeight == undefined) {
		        // default height
		        theHeight = 450;
		    }
		    //alert('params' + theURL + ' ' + theWidth + ' ' + theHeight);
		    //'<iframe src="' + theURL + '" width="710" height="450" frameborder="0"><\/iframe>' +
		    $jq('#js-content').append(
				'<table class="overlay-wrapper '+showClose+'" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>' +
						'<iframe src="' + theURL + '" width="' + theWidth + '" height="' + theHeight + '" frameborder="0"><\/iframe>' +
					'<\/td><\/tr><\/table>' +
				'<\/td><\/tr><\/table>'
			);
		    $jq('html').addClass('overlay-active');
		    $jq('#js-overlay').fadeIn(animspeed);
		    $jq('#js-content').fadeIn(animspeed);
		    return false;
		}
	);

    // popup: closer boxes
    $jq('.closer img').livequery(
		'click',
		function() {
		    $jq('#js-content').trigger('click');
		}
	);

    // page decoration items
    $jq('ul,ol').each(function(x) {
        $jq('li:first', this).addClass('first');
        $jq('li:last', this).addClass('last');
    });
    $jq('body').addClass('with-js');
    $jq('hr').wrap('<div class="hr"><\/div>');
    $jq('table').each(function() {
        $jq('tr:nth-child(even)', this).addClass('even');
        $jq('tr:nth-child(odd)', this).addClass('odd');
    });
    // adding flash to home page
    if ($jq('#doc.home')[0]) {
        homepageFlash();
    }
    // browser specific adjustments:
    // * Opera
    if ($jq.browser.opera) {
        $jq('ul.tabs li a').css({
            'padding': '0 5px'
        });
    }
    // neuromodulation academy popup window links
    $jq('a.nma').bind(
		'click',
		function() {
		    var popWin = window.open(
				$jq(this).attr('href'),
				'popWin',
				'menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=790,height=650'
			);
		    popWin.focus();
		    this.blur();
		    return false;
		}
	);
});


