var player;
function playerReady(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	player = document.getElementById(obj['id']);
};



 // JavaScript Document
$(document).ready(function() {
	$('.inlined + .input-text').each(function(type) {
		$(this).focus(function() {
			$(this).prev('.inlined').addClass('focus');
		});
		$(this).keypress(function() {
			$(this).prev('.inlined').addClass('has-text').removeClass('focus');
		});
		$(this).blur(function() {
			if($(this).val() == '') {
				$(this).prev('.inlined').removeClass('has-text').removeClass('focus');
			}
		});
		
		if($(this).val() != '') {
			$(this).prev('.inlined').addClass('has-text').removeClass('focus');
			if($(this).prev('.inlined').hasClass('has-text') == false) {
				$('.inlined').addClass('has-text');
			}	
		}	
	});
});

// Trade Secrets input fields focus and blur
	$('.inlined + .ts-input-text').each(function(type) {
		$(this).focus(function() {
			$(this).prev('.inlined').addClass('focus');
		});
		$(this).keypress(function() {
			$(this).prev('.inlined').addClass('has-text').removeClass('focus');
		});
		$(this).blur(function() {
			if($(this).val() == '') {
				$(this).prev('.inlined').removeClass('has-text').removeClass('focus');
			}
		});
		
		if($(this).val() != '') {
			$(this).prev('.inlined').addClass('has-text').removeClass('focus');
			if($(this).prev('.inlined').hasClass('has-text') == false) {
				$('.inlined').addClass('has-text');
			}	
		}	
	});
	
	// IE .png catcher
	$('#png-catch').each(function() {
		var originalSrc = $(this).attr('src');
		var splitSrc = originalSrc.split('.');
		if($.browser.msie) {
			$(this).attr('src', splitSrc[0] + '.gif');
		}
	});
 
function goToURL(baseurl,id) {
        
    // temp nav code CP 
    switch (id) {
     case "products": 
        
        window.location.href= baseurl + '/Products/index.aspx';
    break; 
    
    case "parts": 
    window.location.href= baseurl + '/Store/Parts.aspx';
    break; 
    
    case "recipes": 
    window.location.href= baseurl + '/Recipe/index.aspx';
    break; 
    
    case "events": 
    window.location.href= baseurl + '/Events/index.aspx';
    break; 
    
    case "trade_secrets": 
    window.location.href= baseurl + '/Secrets/index.aspx';
    break; 
    
    case "customer_support": 
    window.location.href= baseurl + '/Support/default.aspx';
    break; 
    
    case "store": 
    window.location.href= baseurl + '/Store/index.aspx';
    break; 
    
    case "edge": 
    window.location.href= baseurl + '/Checkout/ShoppingCart.aspx';
    break; 
    
    case "infrared":
    window.location.href= baseurl + '/Infrared/Index.html';
    break; 
    
    case 0:
    window.location.href= baseurl.toString();
    break;
    
    }
    
    return false;
}

function flashTag(file, id, w, h, bg, al, scale, vars, shade){

	var txt = "<object type=\"application/x-shockwave-flash\" width=\""+w+"\" ";
	txt += "height=\""+h+"\"";
	if(id != null) {
		txt += " id=\""+id+"\" name=\""+id+"\"";
	}
	
	txt += " data=\""+file+"\"><param name=\"movie\" value=\""+file+"\" />";
	txt += "<param name=\"allowScriptAccess\" value=\"always\" />";
	if(bg == null) {
		txt += "<param name=\"bgcolor\" value=\"#000000\" />";
	} else if(bg.toLowerCase() == "transparent") {
		txt += "<param name=\"wmode\" value=\"transparent\" />";
	} else{
		txt += "<param name=\"bgcolor\" value=\"#"+bg+"\" />";
	}
	if(scale != null) {
		txt += "<param name=\"scale\" value=\""+scale+"\" />";
	} else{
		txt += "<param name=\"scale\" value=\"noscale\" />";
	}
	if(al != null) {
		txt += "<param name=\"salign\" value=\""+al+"\" />";
	}
	
	if(vars != null) {
		txt += "<param name=\"flashvars\" value=\""+vars+"\" />";
	}
	
	if(shade != null) {
		txt += "<param name=\"shade\" value=\""+shade+"\" />";
	}
	
	document.write(txt);
}



/*	PRODUCT TITLE & WORD COUNT
------------------------------------------------ */
function prodTitle( ttl, w, fsize ) {
	var str = ttl;
	var strLength = Number();
	var output = String();
	var wordArray = str.split(" ");
	var wordCount = wordArray.length;
	var midpoint = Number();
	var cut = Number();
		
	for( var i = 0; i < wordCount; i++ ) {
		strLength += Number(wordArray[i].length);
	}
	
	if( firstLong(wordArray) ) {
		cut = Math.round(wordCount / 2) - 1;
	} else {
		cut = Math.round(wordCount / 2);
	}
		
	midpoint = (strLength * fsize) / 2;	
	if( midpoint > w ) {
		for(var j = 0; j < wordCount; j++) {
			output += wordArray[j];
			if( j != (wordCount - 1) ) {
				output += ' ';
			}
			if( cut <= 3 ) {
				if( j == cut - 1 ) {
					output += '\n';
				}
			} else {
				if( j == cut ) {
					output += '\n';
				}
			}
		}
	} else {
		output = "\n"+ str;
	}
	
	return output;
	
}


function firstLong( array ) {
	var firstHalf = String();
	var secondHalf = String();
	
	for( var i = 0; i < array.length; i++ ) {
		if( i < (Number(array.length) / 2) - 1 ) {
			firstHalf += array[i];
		} else {
			secondHalf += array[i];
		}
	}
	
	firstCount = firstHalf.length;
	secondCount = secondHalf.length;
	
	if( firstCount >= secondCount ) {
		return true;
	} else {
		return false;
	}
}

function prodTitle_OLD(str, w, fsize) {
	var prepStr = str;
	var wordsArray = prepStr.split(' ');
	var wlength = Number(wordsArray.length);
	var newStr = '';
	var strLength = '';
	// Get an approximate width of the string
	for(var i = 0; i < wlength; i++) {
		strLength = Number(strLength + Number(wordsArray[i].length));
	}
	// Set the midpoint
	var midpoint = (strLength * fsize) / 2;
	
	
	if(midpoint > w) {

		for(var j = 0; j < wlength; j++) {
			newStr += wordsArray[j];
			if(j != (wlength - 1)) {
				newStr += ' ';
			}
			if(Math.round(wlength / 2) <= 3) {
				if(j == Math.round((wlength / 2)) - 1) {
					newStr += '\n';
				}
			} else {
				if(j == (Math.round(wlength / 2))) {
					newStr += '\n';
				}
			}
		}
	} else {
		newStr = '\n' + prepStr;
		
	}

	
	return newStr;
}
function wordCount(str) {
	var prepStr = str;
	var wordsArray = prepStr.split(' ');
	var newStr = '';
	if(wordsArray.length > 4) {
		for(var i = 0; i < wordsArray.length; i++) {
			if(wordsArray[i] != '&') {
				newStr += wordsArray[i];
			} else if(wordsArray[i] == '&') {
				newStr += '%26';
			}
			if(i != (wordsArray.length - 1)) {
				newStr += ' ';
			}
			if(i == 2) {
				newStr += '\n';
			}
		}
	} else {
		newStr = '\n' + prepStr;
	}
	return newStr;
}
function largeTitle(ttl) {
	var baseSize = 70;
	var newSize;
	if(ttl.length > 20) {
		newSize = (baseSize / ttl.length) * 20;
	} else {
		newSize = baseSize;
	}
	
	var width = 600;
	var height = Math.round(newSize) + 2;
	var bg = 'transparent';
	var align = 'TL';
	var scale = 'noscale';
	var str = 'size='+ newSize +'&color=e1e1e1&text='+ ttl.toUpperCase();
	var params = Array(width, height, bg, align, scale, str);
	
	var top = -Math.round(newSize - 36) + 'px';
	var marginBtm = top;
	
	$('.title_container').css({ 'top' : top, 'margin-bottom' : marginBtm });
	return(params);
}


// Other Models Rollover

function roll(obj) {
	//alert('rolled');
	
    var model = $(obj);
	var id = model.attr('id');
	//alert($("span[id='"+ id +"'][class='textbox']").length);
	

	
	$("span[id='"+ id +"'][class='textbox']").css({display : 'block' });
	
	model.attr({ src : '../images/'+ id +'.jpg' });
	
}
function out(obj) {
	var model = $(obj);
	var id = model.attr('id');
	$("span[id='"+ id +"'][class='textbox']").css({ display : 'none' });
	if(model.attr('class') != 'current-model') {
		model.attr({ src : '../images/'+ id +'-off.jpg' });
	}
}


// Thumbnails & Descriptions
var isCurrentGrill;
var isCurrentPos = 1;
var currentImg;

$(document).ready(function() { currentImg = $('#start_img').val() });
function swap(obj, id, img) {



//////////////////////////Added by WCB IT

if (document.getElementById("ctl00_Body_pnlVideo")  != null)
{
      stopVideo ();
    document.getElementById('ctl00_Body_pnlImage').className = 'visible'; 
    document.getElementById('ctl00_Body_pnlVideo').className = 'hidden'; 

}

//////////////////////////////////////


	var i = id.split('t');
	var grillViewer = document.getElementById('grillViewer');
	grillViewer.swapImage(img);
	currentImg = img;


//////////////////////////Added by WCB IT
$('.opacity-on').each(function() {
	    //alert(this.id);
	    this.className = 'opacity';
}
);
///////////////////////////////////////////

	$('.thumbnails > img').each(function() {
	  	
	
		var tPos = $(this).attr('id').split('t');
		var isOff = $(this).attr('src').search('-off');
		if(isOff < 0 && tPos[1] != i[1]) {
			conceal($(this), tPos[1]);
		}
	});
	isCurrentGrill = true;
	isCurrentPos = i[1];
	var show = $(obj).attr('src');
	$(obj).attr({ src : show });
	
	$('.desc').each(function() {
		$(this).css({ display : 'none' });
	});
	$('#feature-description-' + i[1]).css({ display : 'block' });
	
	
	//Added by WCB IT
	obj.className = 'opacity-on';
	
	//////////////////
}

function conceal(obj, id) {
	var grillViewer = document.getElementById('grillViewer');
	var removeExt;
	var show;
	
	//Disabled by WCB IT
//	if(isCurrentGrill == false) {
//		grillViewer.swapImage(currentImg);
//		removeExt = $(obj).attr('src').split('.jpg');
//		show = removeExt[0] + '-off.jpg';
//	} else {
//		show = $(obj).attr('src');
//	}



//Added by WCB IT
if(isCurrentGrill == false) {
		grillViewer.swapImage(currentImg);
}
	

//Added by WCB IT
    show = $(obj).attr('src');
    ///////////////
	
	$(obj).attr({ src : show });
	
	var currentFPos = 'feature-description-'+ isCurrentPos;
	
		$('.desc').each(function() {
			if($(this).attr('id') != currentFPos) {
				$(this).css({ display : 'none' });
			} else {
				$(this).css({ display : 'block' });
			}
		});
}

function reveal(obj, id, img) {
//alert('hola');
//alert('reveal');
//alert(id);
	var grillViewer = document.getElementById('grillViewer');
	var currentObj = $(obj);
	var currentThumb = currentObj.attr('src');
	var i = id.split('t');
	var desc = $('#feature-description-' + i[1]);
	var searchOff = currentThumb.search('-off');
	var removeExtention;
	var show;
    
    


		//Disabled by WCB IT
//	if(searchOff > 0) {
//		isCurrentGrill = false;
//		removeExtention = currentThumb.split('-off');
//		show = removeExtention[0] + '.jpg';
//		grillViewer.swapImage(img);
//	} else {
//		isCurrentPos = i[1];
//		isCurrentGrill = true;
//		show = currentThumb;
//	}
//	


//Added by WCB IT
	grillViewer.swapImage(img);
/////////////////	
	
	
	$('.desc').each(function() { 
	
	    $(this).css({display : 'none'}) });
	    desc.css({display : 'block'});
    	currentObj.attr({src : show});

        //Added by WCB IT
        isCurrentGrill = false;
	
    }

function openEmailBox() {
	$('#email_form_container').show();
}

function closeEmailBox() {
	$('#email_form_container').hide();
}

// More info || Products

$(document).ready(function() { show('over', '#overview') });
var lastTtlId = '';
var lastId = '';
function show(thisId, id) {
	if(thisId != lastTtlId) {
		$('#' + thisId).addClass('open');
		if(lastTtlId != '') {
			$('#' + lastTtlId).removeClass('open');
		}
	}
	if(id != lastId) {
		$(id).css({ 'display' : 'block' });
		$(lastId).css({ 'display' : 'none' });
	}
	lastTtlId = thisId;
	lastId = id;
}

//initfinder() // reload selected options based on query string

// Grill Finder Checkboxes
//var searchCriteria = new Array();
var searchCriteria = MDArray(4,0); 

var theList = '';



function initfinder(colTypes,colPrices,colSize,colOptions)
{
var selobj; 

//alert('C'+colTypes + ' P'+colPrices +' Size'+colSize +'Option'+colOptions ) ;

var colLi = document.getElementsByTagName ("LI")
for (var x = 0;x < colLi.length;x++)
{
    switch ( $(colLi.item (x)).attr('gid')) 
        {
             case "0": //Grill Types
        
               if (colTypes.indexOf($(colLi.item (x)).attr('value')) != -1){
               selobj = $("li[gid='"+ $(colLi.item(x)).attr('gid') +"'][value='"+ $(colLi.item(x)).attr('value') +"'][class='unchecked']");
               check(selobj);
               }
               break; 
        
             case "1": // Prices Ranges
        
               if (colPrices.indexOf($(colLi.item (x)).attr('value')) != -1){
               selobj = $("li[gid='"+ $(colLi.item(x)).attr('gid') +"'][value='"+ $(colLi.item(x)).attr('value') +"'][class='unchecked']");
               check(selobj);
               }
                 
               break; 
       
             case "2": // Size
        
               if (colSize.indexOf($(colLi.item (x)).attr('value')) != -1){
               selobj = $("li[gid='"+ $(colLi.item(x)).attr('gid') +"'][value='"+ $(colLi.item(x)).attr('value') +"'][class='unchecked']");
               check(selobj);
               }
                 
               break; 
       
             case "3": // Options
        
               if (colOptions.indexOf($(colLi.item (x)).attr('value')) != -1){
               selobj = $("li[gid='"+ $(colLi.item(x)).attr('gid') +"'][value='"+ $(colLi.item(x)).attr('value') +"'][class='unchecked']");
               check(selobj);
               }
                 
               break; 
       
        }
 }
}

function check(id) {
	var islisted = false;
    var iRowidentifier; 
	var iRowvalue =''; 
	
	iRowidentifier = $(id).attr('gid');
	iRowvalue = $(id).attr('value');
		
		
	if(searchCriteria != '') {
		//searchCriteria.push($(id).html());
		
		// determine which group we are working with 
		 //alert(searchCriteria[iRowidentifier].length);
		 
		for(var i = 0; i < searchCriteria[iRowidentifier].length; i++) {
			//alert(searchCriteria[iRowidentifier].length);
			if(iRowvalue == searchCriteria[iRowidentifier][i]) {
				searchCriteria[iRowidentifier].splice(i, 1);
				islisted = true;
			}
			theList += searchCriteria[iRowidentifier][i] + ', ';
		}
		if(islisted == false) {
			//searchCriteria[iRowidentifier].push($(id).html());
			searchCriteria[iRowidentifier][i] = iRowvalue;
			//alert('index'+i+' - ' +searchCriteria[iRowidentifier][i]);
			theList += iRowvalue; // $(id).html();
		}
	} else {
		//searchCriteria[iRowidentifier].push(iRowvalue);
		searchCriteria[iRowidentifier][0] = iRowvalue;
		//theList += $(id).html();
		theList += iRowvalue;
	}
	//alert('Array: ' + theList);
	$(id).toggleClass('unchecked').toggleClass('checked');
}

//MultiDimensionalArray
function MDArray(iRows,iCols) 
{ 
var i; 
var j; 
   var a = new Array(iRows); 
   for (i=0; i < iRows; i++) 
   { 
       a[i] = new Array(iCols); 
       for (j=0; j < iCols; j++) 
       { 
           a[i][j] = i +'-'+j; 
       } 
   } 
   return(a); 
} 
  
function showResults() {
//	alert('Selected Categories'+searchCriteria[0]);
//    alert('How much spend'+searchCriteria[1]);
//	alert('Cook size'+ searchCriteria[2]);
//	alert('Other Options'+searchCriteria[3]);

 	 var PostResultsURL = 'Grillfinder.aspx?C='+searchCriteria[0]+'&P='+searchCriteria[1]+'&S='+searchCriteria[2]+'&O='+searchCriteria[3]+'&OC='+searchCriteria[3].length;

    // Post back 	 
	goToURL(PostResultsURL,0);
		
}


// Rating Box
function openRatingBox() {
	$('#rating-container').css({ display:'block' });
	$('#rating-container-product').css({ display:'block' });
}
function closeRatingBox() {
	$('#rating-container').css({ display:'none' });
	$('#rating-container-product').css({ display:'none' });
}

// Form Drop Downs
function showSelect() {
	$('.optgroup').show('fast');
}
function chooseOpt(option) {
	if($(option).text() != '') { 
		$('#selected-option').val($(option).text());
		$('.selected-display').text($(option).text());
	} else {
		$('#selected-option').val(option);
		$('.selected-display').text(option);
	}
	$('.optgroup').hide('fast');
}

function blurred(obj, txt) {
	if(obj.value == '') { obj.value = txt } else { obj.value = value }
}
function focused(obj, txt) {
	if(obj.value == txt) { obj.value = '' } else { obj.value = value }
}



//E-commerce Billing page

function EnableShipTo(isChecked) {

  

    if (isChecked != true) {

        document.getElementById("ctl00_Body_txtSFirstName").disabled = false;
        document.getElementById("ctl00_Body_txtSLastName").disabled = false;
        document.getElementById("ctl00_Body_txtSAddress1").disabled = false;
        document.getElementById("ctl00_Body_txtSAddress2").disabled = false;
        document.getElementById("ctl00_Body_txtSCity").disabled = false;
        document.getElementById("ctl00_Body_ddlSState").disabled = false;
        document.getElementById("ctl00_Body_txtSZip").disabled = false;
        document.getElementById("ctl00_Body_txtSPhone").disabled = false;
    }else {


      
        document.getElementById("ctl00_Body_txtSFirstName").disabled = true;
        document.getElementById("ctl00_Body_txtSLastName").disabled = true;
        document.getElementById("ctl00_Body_txtSAddress1").disabled = true;
        document.getElementById("ctl00_Body_txtSAddress2").disabled = true;
        document.getElementById("ctl00_Body_txtSCity").disabled = true;
        document.getElementById("ctl00_Body_ddlSState").disabled = true;
        document.getElementById("ctl00_Body_txtSZip").disabled = true;
        document.getElementById("ctl00_Body_txtSPhone").disabled = true;

    }
}


function openShippingDetails(hid) {

 var openShippingDetails = window.open(hid,'','toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=0,width=550,height=650');
 

}

function playURL(url) {
     
    document.getElementById('ctl00_Body_pnlImage').className = 'hidden'; 
    document.getElementById('ctl00_Body_pnlVideo').className = 'visible'; 
    
    setTimeout("loadVideo('"+url+"');",250);
   
}


function loadVideo(url) {
    player.sendEvent('LOAD', {'file':url, 'type':'video'});
    player.sendEvent("PLAY","true");


}
function stopVideo()
{

 if (document.getElementById('ctl00_Body_pnlVideo').className == 'visible')
    {
        player.sendEvent('STOP');
    }

}

function openCCV(hid) {

    var openCCV = window.open(hid, '', 'toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=0,width=375,height=275');


}



/* Trade Secrets */
var currentVid = 3;
var previousVidID = '#vidthumb3';
function loadNewVideo(vidObj) {
	var videoplayer = document.getElementById('vidplayer');
	var ttl = $('#vid-ttl');
	var desc = $('#vid-desc');
	var grill = $('#vid-grill');
	var recipe = $('#vid-recipe');
	
	videoplayer.changeMovie(vidObj.vid);
	ttl.html(vidObj.title);
	desc.html(vidObj.desc);
	grill.attr('href', vidObj.grill);
	recipe.attr('href', vidObj.recipe);
	$(previousVidID).removeClass('current-vid');
	$(vidObj.id).addClass('current-vid');
	
	previousVidID = vidObj.id;
	
	var num = vidObj.id.split('vidthumb');
	currentVid = Number(num[1]);
}

function vidPrev() {
	var i;
	if(currentVid > 1) {
		i = currentVid - 1;
		loadNewVideo(videos[i]);
	} else if(currentVid <= 1) {
		i = 5;
		loadNewVideo(videos[i]);
	}
}
function vidNext() {
	var i;
	if(currentVid < 5) {
		i = currentVid + 1;
		loadNewVideo(videos[i]);
	} else if(currentVid >= 5) {
		i = 1;
		loadNewVideo(videos[i]);
	}
}

/*	NEW PROD IMAGES
------------------------------------------------ */
var current = '';
var imgCount = '';
function showImg(id) {
	imgCount = imgs.length;
	var idSplit = id.split('img');
	var i = idSplit[1];
	$('#newprod-img-lrg').fadeIn('fast');
	$('#lrg-img-replacement').attr('src', 'images/'+imgs[i]);
	current = i;
}
function closeImg() {
	$('#newprod-img-lrg').fadeOut('fast');
	$('#lrg-img-replacement').attr('src', imgs[0]);
	current = '';
}
function moveThroughImgs(dir) {
	var i = current;
	if(dir == 'prev') {
		if(i == 1) {
			i = imgCount - 1;
		} else {
			i--;
		}
	} else {
		if(i >= imgCount - 1) {
			i = 1;
		} else {
			i++;
		}
	}
	current = i;
	$('#lrg-img-replacement').attr('src', 'images/'+imgs[i]);
}