﻿function equalHeight(groups, elements)
{
	$(groups).each(function() {
		var group = $(this);
		var height = 0;
		var maxHeight = 0;
		var compensationHeight = 0;

		group.find(elements).each(function() {
			element = $(this);
			height = element.height();
			if (element.parent().find(".boxLink").length > 0) {
				height += 7;
			}
			if (element.parent().find(".boxImage").length > 0) {
				compensationHeight = element.parent().find(".boxImage").height() + 5; //boxImage have padding-top: 5px
				height += compensationHeight;
			}
			maxHeight = (height > maxHeight) ? height : maxHeight;
		});

		group.find(elements).each(function() {
			var element = $(this);
			var element_to_set = element;
			if (element.find(".boxContentInner2").length > 0)
				element_to_set = element.find(".boxContentInner2");

			var property = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';

			if (element.parent().find(".boxImage").length > 0)
				element_to_set.css(property, (maxHeight - compensationHeight) + 'px');
			else
				element_to_set.css(property, (maxHeight) + 'px');
		});

	});
}

function reInitScriptsAfterAjax() { init(); }

function init() {
	//Cufon
	if(isCufonAble())
		Cufon.replace("#chart h3, .productList h3, .productList h4, #accessories h3");

	//Equal height columns. The order IS important! (boxContent after innerPadding)
	equalHeight(".content > .yui-ge", ".innerPadding");
	equalHeight(".content > .yui-ge", ".boxContentInner");
	equalHeight(".content > .yui-g", ".innerPadding");
	equalHeight(".content > .yui-g", ".boxContentInner");
	equalHeight("#accessories", "ul");
}

$(document).ready(function() {

	$(document).ajaxSuccess(function(evt, request, settings) { reInitScriptsAfterAjax(); });

	//Functions needed in a separate function that can be called after an ajax call, re-running necessary scripts
	init();

	//Form buttons
	$(".action_buttons_backup, .action_buttons").toggle();

	//Nice checkboxes
	if ($("input[type=checkbox]").length > 0 || $("input[type=radio]").length > 0) {
		$("input").checkBox();
	}

	//Filter toggler tab
	$("a#narrow_it_down").click(function() {
		$("#filter").slideToggle("fast");
		$(this).toggleClass("active");
	});

	//Open modal box
	$("#popup").click(function(event) {
	var params = eval("(" + $(this).attr("params") + ")");
		var url = params.url || this.href;
		url += url.indexOf("?") > -1 ? "&" : "?";
		url += "brandColor=" + hasBrandStyleClass();
		
		popup.openLayer(params.id.toLowerCase(), url, "enhancedpresentation", params.width, params.height);

		event.stopPropagation();
		return false;
	});

	//Filter: Clear all selection
	$("a#clear_filter_checkboxes").click(function() {
		$("#filter input:checkbox").removeAttr("checked").checkBox('reflectUI');
	});

	//Form button events
	$(".action_button.submit").click(function() {
		$(this).closest("form").submit();
	});

	//JC Player
	$(".jcPlayer-open").click(function(event) {
		event.preventDefault();
		var flv = $(this).attr("href");
		$("#jcPlayer-embed").html(createJCPlayer(flv));
		$("#jcPlayer").show();
		window.scroll(0, 0);
		return false;
	});
	$("#jcPlayer-close").click(function() {
		$("#jcPlayer").hide();
		$("#jcPlayer-embed").html("");
	});
});

function createJCPlayer(flv) {
	rootPath = rootPath || "";
	hex = rgb2hex($("body .color").css("color"));

	return AC_FL_RunContent(
	'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
	'width', '640',
	'height', '385',
	'src', rootPath + 'Content/JCPlayer/jcplayer',
	'quality', 'high',
	'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	'align', 'middle',
	'play', 'true',
	'loop', 'true',
	'scale', 'noScale',
	'wmode', 'window',
	'devicefont', 'false',
	'id', 'jcplayer',
	'bgcolor', '#ffffff',
	'name', 'jcplayer',
	'menu', 'true',
	'allowFullScreen', 'true',
	'allowScriptAccess','sameDomain',
	'FlashVars', 'videoURL=' + flv + '&autoPlay=false&backgroundColor1=0x000000&backgroundColor2=0x444444&highlightColor=0x' + hex + '&fullScreenButton=false',
	'movie', rootPath + 'Content/JCPlayer/jcplayer',
	'salign','TL'
	); //end AC code
	
	//videoURL is a relative path from jcplayer.swf, and not from the html/view.
	
	//Settable JCPlayer params :startPhotoSource,videoURL,loop,autoPlay,scaleMode,volume,bufferTime,backgroundColor1,movieBackgroundColor,backgroundColor2,highlightColor,inkColor,playButton,timeBar,seekBar,volumeButton,fullScreenButton,skin,smoothing,autoHide,autoHideFullScreen,offsetY,offsetYFullScreen,margins,marginsFullScreen
}

function rgb2hex(rgb)
{  
	rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);  
	function hex(x) {  
		hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");  
		return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];  
	}  
	return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);  
}

function isCufonAble()
{
	var B = ["he","zh","th","el","ar","vi","ko","fa"];
	for(var A in B)
	{
		if(lc.toLowerCase() == B[A])
		{
			return false;
		}
	}
	return true;
}
