/**
 * Common JS for 111pix.com @ Encommerce3.0
 * (c) Envisionext, http://www.envisionext.com
 */

/* initialize for other functions - loaded at the end of body */
function init111() {	
	//if (navigator.platform.indexOf('Linux') == -1) sIFR.initialize();  sifr is not for linux jet
	loginFormUsability();
	externalLinks();
	
	//Nifty("ul#mainmenu a","small transparent"); // round corners	
	//Rounded('#mainmenu a', {'radius':3, 'hover':'#6b7d8f'});
	//Rounded('#b2b_menu a', {'radius':3, 'hover':'#6b7d8f'});
	//Rounded('#dvdDownload', {'radius':5});
	//if (document.all && navigator.appVersion.indexOf('MSIE 7.0')==-1 && !window.opera) {
		//window.onload = function() {Corners('.grey_border');}
	//} else {
	//	Corners('.grey_border');
	//}
}

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

/* for valid 'open in new window' links */
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
 }
}

/* some useful magic for login-form */
function loginFormUsability() {
	if (loginFld = $('loginForm_login')){
		if(loginFld.value==''){
			loginFld.value='Login';
		}
	}
	if (psw = $('loginForm_passwd')){
		if(psw.value==''){
			psw.value='Password';
		}
		if(!document.all) {
			psw.type = psw.value=='Password' ? 'text' : 'password';
		}
	}
}

/* hiding and unhinding by id */
function toggler(id) {
	if (!id) return false;
	if (!(el = $(id))) {
		return false;
	} else {
		if (el.offsetHeight) {
			el.style.display = 'none';
			return -1;
		} else {
			el.style.display = '';
			return 1;
		}
	}
}

/*Function to show splash window. We will show splash Window */

function showSplash()
{
	
	curUrl = window.location.href 
	if ( curUrl.indexOf('basket.php')==-1 || curUrl.indexOf('content.php')==-1 || curUrl.indexOf('login.php')==-1)
	{
		if (getCookie('start_state') != '1')
		{
			showAny('intro', true, true); 
			setCookie('start_state', '1', '3000');
		}
	}
	
	if (curUrl.indexOf('login')>1)
	{
		if (getCookie('login_state') != '1')
		{
			showAny('user_splash', true, true); 
			setCookie('login_state', '1', '3000');
		}
	}
	
	if (curUrl.indexOf('basket')>1)
	{
		if (getCookie('basket_state') != '1') {
			showAny('basket_splash', true, true);
			setCookie('basket_state', '1', '3000');
		}
	}
	
	if (curUrl.indexOf('content')>1)
	{
		if ($('adult_splash'))
		{
			showAny('adult_splash', false, false);
		}
	}
}

/* */

/* display some div in js-popup almost like lightbox displays images */
function showAny(id, noCancel, keyClose) {
	if (!(el = $(id))) return false;
	flag = noCancel ? true : false;
	flag_2 = keyClose ? true : false;
	var objBody = document.getElementsByTagName("body").item(0);
	
	if (!(objOverlay = $('showAnyOverlay'))) {
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','showAnyOverlay');
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay)
		
		var objW = document.createElement("div");
		objW.setAttribute('id','showAnyWrap');
		objW.style.display = 'none';
		
		
		var objContent = document.createElement("div");
		objContent.setAttribute('id','showAnyContent');
		objContent.className = 'clearfix';
		objContent.appendChild(el);	
		objW.appendChild(objContent);
		
		var objBottomNav = document.createElement("div");
		objBottomNav.setAttribute('id','showAnyBottom');
		objBottomNav.style.display = 'none';
		objW.appendChild(objBottomNav);
	
		var objBottomNavCloseLink = document.createElement("a");
		objBottomNavCloseLink.setAttribute('id','showAnyClose');
		objBottomNavCloseLink.setAttribute('href','#');
		objBottomNavCloseLink.onclick = function() { showAnyCancel(); return false; }
		objBottomNavCloseLink.appendChild(document.createTextNode('Close this'));
		objBottomNav.appendChild(objBottomNavCloseLink);
		objBody.appendChild(objW);		
	} else { $('showAnyContent').appendChild(el); }
	
	if(flag){
		objOverlay.onclick = function() { showAnyCancel(); return false; } /*: objOverlay.onclick = function() {};*/
	}
	if (flag_2){
		objBody.onkeypress = function() { showAnyCancel(); return false; }
		document.onkeypress = function() { showAnyCancel(); return false; }
	}
	
	hideSelectBoxes();
	$('showAnyWrap').style.zIndex = '-100';
	
	Element.show(el);
	Element.show('showAnyWrap');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	wCur = Element.getWidth(el);
	var showAnyLeft = arrayPageScroll[0] + parseInt(arrayPageSize[2]/2 - wCur/2);
	var showAnyTop = arrayPageScroll[1] + (arrayPageSize[3]/15);
	Element.setHeight('showAnyOverlay', arrayPageSize[1]);
	Element.setWidth('showAnyWrap', wCur);
	Element.setLeft('showAnyWrap',showAnyLeft);
	if(!arguments[3])Element.setTop('showAnyWrap', showAnyTop);	
	
	new Effect.Appear('showAnyOverlay', { duration: 0.2, from: 0.0, to: 0.4 });	
	$('showAnyWrap').style.zIndex = '1000';
	
	if (!flag && !arguments[3]) new Effect.Parallel(
				[ new Effect.SlideDown( 'showAnyBottom', { sync: true, duration: resizeDuration + 0.25, from: 0.0, to: 1.0 }), 
					new Effect.Appear('showAnyBottom', { sync: true, duration: 1.0 }) ], 	{ duration: 0.65 } );
}
/* hide upper js-popup */
function showAnyCancel() {
	if (!(objW = $('showAnyOverlay'))) return false;
	
	Element.hide('showAnyBottom');
	Element.hide('showAnyWrap');
	el = $('showAnyContent').firstChild;
	Element.hide(el);
	var objBody = document.getElementsByTagName("body").item(0);
	objBody.appendChild(el);
	
	new Effect.Fade('showAnyOverlay', { duration: 0.2});
	showSelectBoxes();
}

/* make simple corners for divs */
function Corners(selector) {
	if (!selector) return false;
	var names = Array('lt','rt','lb','rb');
	var el = parseSelector(selector);
	var elNum = el.length;
	for (var i=elNum-1; i>=0; i--) {
		//addWrap(el[i]);
		if (typeof(getComputedStyle)=='function') el[i].currentStyle = getComputedStyle(el[i], null);

		el[i].style.position = el[i].currentStyle.position=='absolute' ? 'absolute' : 'relative';
		makeEvenSizes(el[i]);
		
		for(j=0; j<4; j++) {
			var corner = document.createElement('b');
			corner.className = 'corn corner_'+names[j];
			corner.style.display='block';
			corner.style.position='absolute';
			corner.style.overflow='hidden';
			switch (names[j]) {
				case 'lt':
					corner.style.left = el[i].currentStyle.borderLeftWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderLeftWidth;
					corner.style.top = el[i].currentStyle.borderTopWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderTopWidth;
				break;
				case 'rt':
					corner.style.right = el[i].currentStyle.borderRightWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderRightWidth;
					corner.style.top = el[i].currentStyle.borderTopWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderTopWidth;
				break;
				case 'lb':
					corner.style.left = el[i].currentStyle.borderLeftWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderLeftWidth;
					corner.style.bottom = el[i].currentStyle.borderBottomWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderBottomWidth;
				break;
				case 'rb':
					corner.style.right = el[i].currentStyle.borderRightWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderRightWidth;
					corner.style.bottom = el[i].currentStyle.borderBottomWidth=='medium' ? '0' : '-'+el[i].currentStyle.borderBottomWidth;
				break;
			}
			el[i].appendChild(corner);
		}
	}
}
/* for correct display of absolute corners in stupid ie */
function makeEvenSizes(el, mod) {
	if (!el.tagName) return false;
	mod = mod ? mod : '';
	switch (mod) {
		case 'ie':
			forBro = document.all && !window.opera;
		break;
		case 'ie7':
			forBro = navigator.appVersion.indexOf('MSIE 7.0')==-1;
		break;
		case 'opera':
			forBro = document.all && !window.opera;
		break;
		case 'all':
			forBro = true;
		break;
		default: // for ie<7
			forBro = document.all && navigator.appVersion.indexOf('MSIE 7.0')==-1 && !window.opera;
		break;		
	}
	
	if (forBro) {
		el.defWidth = el.defWidth ? el.defWidth : el.currentStyle.width;
		el.defWHeight = el.defWHeight ? el.defWHeight : el.currentStyle.height;
		
		el.style.width = el.defWidth;
		el.style.height = el.defWHeight;
		w = el.offsetWidth;
		h = el.offsetHeight;
		
		wN = w - parseInt(el.currentStyle.paddingRight) - parseInt(el.currentStyle.paddingLeft) - parseInt('0'+el.currentStyle.borderLeftWidth) - parseInt('0'+el.currentStyle.borderRightWidth);
		el.style.width = (w % 2) ? (wN+1)+'px' : wN+'px';
		
		hN = h - parseInt(el.currentStyle.paddingTop) - parseInt(el.currentStyle.paddingBottom) - parseInt('0'+el.currentStyle.borderTopWidth) - parseInt('0'+el.currentStyle.borderBottomWidth);
		el.style.height = (h % 2) ? (hN+1)+'px' : hN+'px';
	}
	return true;
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}