
var $j = jQuery.noConflict();

var BrowserDetect = {
	init : function() {
		this.browser = this.searchString(this.dataBrowser)
				|| "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString : function(data) {
		for ( var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch
					|| data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			} else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion : function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1)
			return;
		return parseFloat(dataString.substring(index
				+ this.versionSearchString.length + 1));
	},
	dataBrowser : [ {
		string :navigator.userAgent,
		subString :"Chrome",
		identity :"Chrome"
	}, {
		string :navigator.userAgent,
		subString :"OmniWeb",
		versionSearch :"OmniWeb/",
		identity :"OmniWeb"
	}, {
		string :navigator.vendor,
		subString :"Apple",
		identity :"Safari"
	}, {
		prop :window.opera,
		identity :"Opera"
	}, {
		string :navigator.vendor,
		subString :"iCab",
		identity :"iCab"
	}, {
		string :navigator.vendor,
		subString :"KDE",
		identity :"Konqueror"
	}, {
		string :navigator.userAgent,
		subString :"Firefox",
		identity :"Firefox"
	}, {
		string :navigator.vendor,
		subString :"Camino",
		identity :"Camino"
	}, { // for newer Netscapes (6+)
				string :navigator.userAgent,
				subString :"Netscape",
				identity :"Netscape"
			}, {
				string :navigator.userAgent,
				subString :"MSIE",
				identity :"Explorer",
				versionSearch :"MSIE"
			}, {
				string :navigator.userAgent,
				subString :"Gecko",
				identity :"Mozilla",
				versionSearch :"rv"
			}, { // for older Netscapes (4-)
				string :navigator.userAgent,
				subString :"Mozilla",
				identity :"Netscape",
				versionSearch :"Mozilla"
			} ],
	dataOS : [ {
		string :navigator.platform,
		subString :"Win",
		identity :"Windows"
	}, {
		string :navigator.platform,
		subString :"Mac",
		identity :"Mac"
	}, {
		string :navigator.platform,
		subString :"Linux",
		identity :"Linux"
	} ]

};

BrowserDetect.init();

function changeInputType(oldElm, // a reference to the input element
		iType, // value of the type property: 'text' or 'password'
		iValue, // the default value, set to 'password' in the demo
		blankValue, // true if the value should be empty, false otherwise
		noFocus) { // set to true if the element should not be given focus

	if (!oldElm || !oldElm.parentNode || (iType.length < 4)
			|| !document.getElementById || !document.createElement)
		return;
	var isMSIE = /* @cc_on!@ */false; // http://dean.edwards.name/weblog/2007/03/sniff/

	if (!isMSIE) {
		var newElm = document.createElement('input');
		newElm.type = iType;
	} else {
		var newElm = document.createElement('span');
		newElm.innerHTML = '<input type="' + iType + '" name="' + oldElm.name
				+ '">';
		newElm = newElm.firstChild;
	}
	var props = [ 'name', 'id', 'className', 'size', 'tabIndex', 'accessKey' ];
	for ( var i = 0, l = props.length; i < l; i++) {
		if (oldElm[props[i]])
			newElm[props[i]] = oldElm[props[i]];
	}
	newElm.onfocus = function() {
		return function() {
			if (this.hasFocus)
				return;
			var newElm = changeInputType(this, 'password', iValue, (this.value
					.toLowerCase() == iValue.toLowerCase()) ? true : false);
			if (newElm)
				newElm.hasFocus = true;
		}
	}();
	newElm.onblur = function() {
		return function() {
			if (this.hasFocus)
				if (this.value == ''
						|| (this.value.toLowerCase() == iValue.toLowerCase())) {
					changeInputType(this, 'text', iValue, false, true);
				}
		}
	}();
	// hasFocus is to prevent a loop where onfocus is triggered over and
	// over again
	newElm.hasFocus = false;
	// some browsers need the value set before the element is added to the
	// page
	// while others need it set after
	if (!blankValue)
		newElm.value = iValue;
	oldElm.parentNode.replaceChild(newElm, oldElm);
	if (!isMSIE && !blankValue)
		newElm.value = iValue;
	if (!noFocus || typeof (noFocus) == 'undefined') {
		window.tempElm = newElm;
		setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
	}
	return newElm;
}

function MM_showHideLayers() {
	var i, v, obj, args = MM_showHideLayers.arguments;
	for (i = 0; i < (args.length - 2); i += 3)
		if ((obj = document.getElementById(args[i])) != null) {
			v = args[i + 2];
			if (obj.style) {
				obj = obj.style;
				v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v;
			}
			obj.visibility = v;
		}
}

function change_class(obj) {
	document.getElementById(obj).className = (document.getElementById(obj).className == 'more_description2') ? 'more_description'
			: 'more_description2';
}

function changetext(short_text, long_text, pObj) {
	$j(pObj).text($j(pObj).text() == short_text ? long_text : short_text);
}

function closeShader(popup) {
	$j('#shade').stop().fadeTo(1000, 0, function () { $j(this).css("visibility", "hidden"); $j(this).css("height", "1px")});
	$j(popup).stop().slideUp("slow", function () { $j(this).css("visibility", "hidden")});
	currentPopupId="";
}

var currentPopupId = "";

function popup(popupId) {
	position();
	resizeShader();

	currentPopupId = popupId;
	$j(popupId).stop().slideDown('slow');
	$j('#shade').stop().fadeTo('slow', 0.5);
	
	$j(popupId).css("visibility", "visible");
	$j('#shade').css("visibility", "visible");
	
}

function resize() {

	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) {
		prop = 'height';
	} else {
		prop = 'min-height';
	}

	$j('div.ed_content').css('height', 'auto');
	$j('div.ed_content').css(prop, '1px');
	$j('div.ed_content').css(prop,$j('div.ed_content').height() + $j('div.wraper').height()- $j('div.container').height() - $j('div.ed_footer').height()- 50);

}

function position() {
	var selector = currentPopupId!=""?currentPopupId:"div.popup";
	if(document.documentElement.clientHeight > $j(selector).height() + 40){
		v1 = function () { 
			var selector = currentPopupId!=""?currentPopupId:"div.popup";
			scrollTop = $j(window).scrollTop();
			$j(selector).css("top", scrollTop);
			$j(selector).css("margin-top", 20);
		};
		v1();
		$j(window).scroll( v1 );
	} else {
		scrollTop = $j(window).scrollTop();
		$j(selector).css("top", scrollTop);
		mTop = Math.round((document.documentElement.clientHeight-$j(selector).height())/2);
		$j(selector).css("margin-top", (mTop>0)?mTop:0);
	}
}

$j(window).resize( function() {
	resize();
	$j(window).unbind('scroll');
	position();

	resizeShader();
});

function resizeShader() {
	h = $j('div.container').height() + $j('div.ed_footer').height() + 50;
	$j('#shade').css("height",h);
}

$j(document).ready( function() {
			if($j('div.tx-fbmagento input[type=radio]') != null){
				$j('div.tx-fbmagento input[type=radio]').addClass("magento_radio_button");
				
			}
	
			/*
			 * Logic for popup window
			 */
			$j('div.popup').slideUp("slow");
			$j('#shade').fadeTo(100, 0);
			
			$j('div.shade').after($j('div.popup'));
			
			if($j('div.popup input[type=submit]') != null){
				$j('div.popup input[type=submit]').addClass("form_button");
			}
			
			position();
			
			/*
			 * Logic for resize window
			 */
			resize();

			/*
			 * Logic for change input type
			 */
			if ($j('#pass') != null && $j('#pass') != "null") {
				var ua = navigator.userAgent.toLowerCase();

				if (!((ua.indexOf('konqueror') != -1) && /khtml\/3\.[0-4]/
						.test(ua))
						&& !(((ua.indexOf('safari') != -1) && !window.print))) {
					// Set the third value to the text you want to appear in the field.
					changeInputType(document.getElementById('pass'), 'text', 'lozinka', false, true);
				}
			}
		

			/*
			 * Logic for input fields
			 */
			$j('input[type="text"]').focus( function() {
				$j(this).addClass("focusField")

				if (this.value == this.defaultValue) {
					this.value = '';
				}
				if (this.value != this.defaultValue) {
					this.select();
				}

			});
			$j('input[type="text"]').blur( function() {

				if (this.value == '') {

					$j(this).removeClass("focusField")
					this.value = this.defaultValue;

				}
			});
			
			if($j('#message')){
			$j('div.application').click( function() {
				$j('#message').hide();
			});
			}

			/*
			 * Logic for show user/pass
			 */
			$j('div.application a').click( function() {
				$j('#login').show(300);

			});

			/*
			 * Logic for show menu
			 */

			var teasers = $j('li.firstLevel');

			var totalWidth = 0;

			teasers.each( function(i) {
				totalWidth += teasers[i].offsetWidth;
				totalWidth += 1;
			});

			if (981 > totalWidth) {
				/*
				 * Add additional padding
				 */

				var addPadding = (981 - totalWidth - teasers.length)
						/ (teasers.length * 2);
				var cumulatedError = 0.0;

				teasers.each( function(i) {
					add = Math.floor(addPadding);
					cumulatedError += addPadding - Math.floor(addPadding);
					if (cumulatedError > 1) {
						cumulatedError--;
						add++;
					}

					$j(teasers[i]).css('width', teasers[i].offsetWidth + add);

					add = Math.floor(addPadding);
					cumulatedError += addPadding - Math.floor(addPadding);
					if (cumulatedError > 1) {
						cumulatedError--;
						add++;
					}

					$j(teasers[i]).css('width', teasers[i].offsetWidth + add);
				});

				teasers.each( function(teaser) {
					// teaser.getElement('a.first').setStyle('width',
						// teaser.offsetWidth);
					});

				/*
				 * Check if everything went fine, if not correct (by maximum 1px)
				 * Note: this should not happen at all
				 */

				totalWidth = 0.0;
				teasers.each( function(i) {
					totalWidth += teasers[i].offsetWidth;
					// alert(teaser.offsetWidth);
					});
				// alert(totalWidth);
			}
			;

			/*
			 * Logic for slide in\out
			 */

			var list = $j('div.activity_text_long');
			var headings = $j('div.more_description a');
			list.hide();

			headings.each( function(i) {
				$j(headings[i]).click( function() {
					$j(list[i]).toggle(400);
				});

			});

});


function tweakFlashMozilla() {
	$j("#teaser_1").css("opacity", "0.9998");
	$j("#teaser_2").css("opacity", "0.9998");
	$j("#teaser_3").css("opacity", "0.9998");
	$j("#teaser_4").css("opacity", "0.9998");
	$j("#teaser_5").css("opacity", "0.9998");
	
	if ($j("#teaser_1")) {
		$j("#teaser_1").bind("mouseenter",function(){
	      $j("#teaser_1").css("opacity", "0.9999");
	    }).bind("mouseleave",function(){
	      $j("#teaser_1").css("opacity", "0.9998");
	    });
	}
	if ($j("#teaser_2")) {
		$j("#teaser_2").bind("mouseenter",function(){
	      $j("#teaser_2").css("opacity", "0.9999");
	    }).bind("mouseleave",function(){
	      $j("#teaser_2").css("opacity", "0.9998");
	    });
	}
	if ($j("#teaser_3")) {
		$j("#teaser_3").bind("mouseenter",function(){
	      $j("#teaser_3").css("opacity", "0.9999");
	    }).bind("mouseleave",function(){
	      $j("#teaser_3").css("opacity", "0.9998");
	    });
	}
	if ($j("#teaser_4")) {
		$j("#teaser_4").bind("mouseenter",function(){
	      $j("#teaser_4").css("opacity", "0.9999");
	    }).bind("mouseleave",function(){
	      $j("#teaser_4").css("opacity", "0.9998");
	    });
	}
	if ($j("#teaser_5")) {
		$j("#teaser_5").bind("mouseenter",function(){
	      $j("#teaser_5").css("opacity", "0.9999");
	    }).bind("mouseleave",function(){
	      $j("#teaser_5").css("opacity", "0.9998");
	    });
	}
}



function tweakFlashOpera() {
	$j("#teaser_1").css("z-index", "2");
	$j("#teaser_2").css("z-index", "2");
	$j("#teaser_3").css("z-index", "2");
	$j("#teaser_4").css("z-index", "2");
	$j("#teaser_5").css("z-index", "2");
	
	if ($j("#teaser_1")) {
		$j("#teaser_1").bind("mouseenter",function(){
	      $j("#teaser_1").css("z-index", "1");
	    }).bind("mouseleave",function(){
	      $j("#teaser_1").css("z-index", "2");
	    });
	}
	if ($j("#teaser_2")) {
		$j("#teaser_2").bind("mouseenter",function(){
	      $j("#teaser_2").css("z-index", "1");
	    }).bind("mouseleave",function(){
	      $j("#teaser_2").css("z-index", "2");
	    });
	}
	if ($j("#teaser_3")) {
		$j("#teaser_3").bind("mouseenter",function(){
	      $j("#teaser_3").css("z-index", "1");
	    }).bind("mouseleave",function(){
	      $j("#teaser_3").css("z-index", "2");
	    });
	}
	if ($j("#teaser_4")) {
		$j("#teaser_4").bind("mouseenter",function(){
	      $j("#teaser_4").css("z-index", "1");
	    }).bind("mouseleave",function(){
	      $j("#teaser_4").css("z-index", "2");
	    });
	}
	if ($j("#teaser_5")) {
		$j("#teaser_5").bind("mouseenter",function(){
	      $j("#teaser_5").css("z-index", "1");
	    }).bind("mouseleave",function(){
	      $j("#teaser_5").css("z-index", "2");
	    });
	}
}

$j(document).ready(function() {
	if (BrowserDetect.browser != "Explorer" && $j("#teaser_1") && $j("#teaser_2") && $j("#teaser_3") && $j("#teaser_4") && $j("#teaser_5") && swfobject) {
		if (BrowserDetect.browser == "Opera" )
			swfobject.addLoadEvent(tweakFlashOpera);
		else
			swfobject.addLoadEvent(tweakFlashMozilla);
	}
});

