// Browser Detect  v2.1.6 - (slightly modified for Prototype consistency by Phosworks AB 060418)
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)
var BrowserDetect = Class.create();
BrowserDetect.prototype = {
	initialize: function() {
		var ua = navigator.userAgent.toLowerCase(); 
		
		// browser engine name
		this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
		this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);
		
		// browser name
		this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
		this.isSafari      = (ua.indexOf('safari') != - 1);
		this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
		this.isOpera       = (ua.indexOf('opera') != -1); 
		this.isIcab        = (ua.indexOf('icab') != -1); 
		this.isAol         = (ua.indexOf('aol') != -1); 
		this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
		this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
		this.isFirebird    = (ua.indexOf('firebird/') != -1);
		this.isFirefox	   = (ua.indexOf('firefox/') != -1);
		this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
		
		// spoofing and compatible browsers
		this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
		this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
		
		// rendering engine versions
		this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
		this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
		this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
		
		// browser version
		this.versionMinor = parseFloat(navigator.appVersion); 
		
		// correct version number
		if (this.isGecko && !this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
		}
		else if (this.isMozilla) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
		}
		else if (this.isIE && this.versionMinor >= 4) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
		}
		else if (this.isKonqueror) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
		}
		else if (this.isSafari) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
		}
		else if (this.isOmniweb) {
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
		}
		else if (this.isOpera) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
		}
		else if (this.isIcab) {
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
		}
		
		this.versionMajor = parseInt(this.versionMinor); 
		
		// dom support
		this.isDOM1 = (document.getElementById);
		this.isDOM2Event = (document.addEventListener && document.removeEventListener);
		
		// css compatibility mode
		this.mode = document.compatMode ? document.compatMode : 'BackCompat';
		
		// platform
		this.isWin    = (ua.indexOf('win') != -1);
		this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
		this.isMac    = (ua.indexOf('mac') != -1);
		this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
		this.isLinux  = (ua.indexOf('linux') != -1);
		
		// specific browser shortcuts
		this.isNS4x = (this.isNS && this.versionMajor == 4);
		this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
		this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
		this.isNS4up = (this.isNS && this.versionMinor >= 4);
		this.isNS6x = (this.isNS && this.versionMajor == 6);
		this.isNS6up = (this.isNS && this.versionMajor >= 6);
		this.isNS7x = (this.isNS && this.versionMajor == 7);
		this.isNS7up = (this.isNS && this.versionMajor >= 7);
		
		this.isIE4x = (this.isIE && this.versionMajor == 4);
		this.isIE4up = (this.isIE && this.versionMajor >= 4);
		this.isIE5x = (this.isIE && this.versionMajor == 5);
		this.isIE55 = (this.isIE && this.versionMinor == 5.5);
		this.isIE5up = (this.isIE && this.versionMajor >= 5);
		this.isIE6x = (this.isIE && this.versionMajor == 6);
		this.isIE6up = (this.isIE && this.versionMajor >= 6);
		
		this.isIE4xMac = (this.isIE4x && this.isMac);	
	}
}
var Browser = new BrowserDetect();

var IEFixer = {
	/*aPngImgs: [],*/
	
	FixIE: function() {
		if ( document.getElementsByTagName && document.createElement ) {
			//this.getPngImages();
			//this.replacePngImages();
			this.fixGlobalMenu();
			if ( Browser.isIE6x ) {
				this.fixButtonSuckerFish();
				this.fixMenuSuckerFish();
			}
		}
	},
	
	/*
	getPngImages: function() {
		var tempImg = document.getElementsByTagName('img');
		for ( i=0; i<tempImg.length; i++ ) {
			if ( /.*\.png$/.test(tempImg[i].src) ) this.aPngImgs.push(tempImg[i]);
		}
	},
	
	replacePngImages: function() {
		for ( i=0; i<this.aPngImgs.length; i++ ) {
			var imgDim = Element.getDimensions(this.aPngImgs[i]);
			
			var pngDiv = document.createElement('div');
			pngDiv.className = this.aPngImgs[i].className;
			Element.setStyle(pngDiv, {width: imgDim.width, height: imgDim.height});
			Element.setStyle(pngDiv, {filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.aPngImgs[i].src+"', sizingMethod='crop')"});
			
			this.aPngImgs[i].parentNode.replaceChild(pngDiv, this.aPngImgs[i]);
		}
	},
	*/
	
	fixGlobalMenu: function() {
	    var globalMenu = p$('gMenu');
	    if ( globalMenu && document.getElementsByTagName ) {
	        var listItems = globalMenu.getElementsByTagName('li');
            
            for(i=0; i<listItems.length; i++) {
                if ( i>0 ) new Insertion.Before(listItems[i], "<span class=\"sep\">|</span>");
            }
	    }
	},

	fixButtonSuckerFish: function() {
		var buttons = document.getElementsByClassName('submitButton');
		var buttons2 = document.getElementsByClassName('graphicButton'); 
		buttons2.each( function(item) {
			buttons.push(item);
		});
		for(i=0;i<buttons.length;i++) {
			buttons[i].onmouseover = function() {
				this.className += " bhover";
			}
			buttons[i].onmouseout = function() {
				this.className=this.className.replace(new RegExp(" bhover\\b"), "");
			}
		}
	},
	
	fixMenuSuckerFish: function() {
		var mItem = p$('mainMenu');
		var cNodeCount = mItem.childNodes.length;
		for( i=0; i<cNodeCount; i++ ) {
			if ( mItem.childNodes[i].className.indexOf('active')==-1 ) {
				mItem.childNodes[i].onmouseover = function() {
					this.className += " mhover";
				}
				mItem.childNodes[i].onmouseout = function() {
					this.className=this.className.replace(new RegExp(" mhover\\b"), "");
				}
			}
		}
	}
}
if ( Browser.isIE ) Event.observe(window, 'load', IEFixer.FixIE.bind(IEFixer), false);

var FlashRenderer = {        
    RenderGenericFlash: function(strSrc, strWidth, strHeight, transparent, id) {
        AC_FL_RunContent('codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width',strWidth,'height',strHeight,'align','middle','src',strSrc,'quality','highest','bgcolor','#ffffff','allowScriptAccess','always','pluginspage','http://www.macromedia.com/go/getflashplayer','movie', strSrc, 'allowfullscreen', 'true', 'wmode', transparent?'transparent':'window', 'id', id?id:'' );
    },
    
    RenderGenericFlash2: function(strSrc, strWidth, strHeight, bgcolor, transparent, id) {
//        AC_FL_RunContent('codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width',strWidth,'height',strHeight,'align','middle','src',strSrc,'quality','highest','bgcolor',bgcolor,'allowScriptAccess','always','pluginspage','http://www.macromedia.com/go/getflashplayer','movie', strSrc, 'allowfullscreen', 'true', 'wmode', transparent?'transparent':'window', 'id', id?id:'' );    
			AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0',
			'width', strWidth,
			'height', strHeight,
			'src', strSrc,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'scale', 'showall',
			'wmode', transparent?'transparent':'window',
			'devicefont', 'true',
			'id',  id?id:'',
			'bgcolor', bgcolor,
			'name',  id?id:'',
			'menu', 'true',
			'allowScriptAccess','always',
			'allowFullScreen','true',
			'salign', ''
		);
    }
}

var PopupManager = {
	OpenScreenViewer: function(prod_id, cur) {
		window.open('/ProductsScreenshotsViewer.aspx?id='+prod_id+'&cur='+cur,'screenViwer','width=800,height=600,scrollbars=no,toolbar=no,resizable=no,status=no');
	},
	
	OpenScreenViewer2: function(prod_id, cur, plat_id, pubdate) {
		window.open('/ProductsScreenshotsViewer.aspx?id='+prod_id+'&cur='+cur+'&plat_id='+plat_id+'&pubdate='+pubdate,'screenViwer','width=800,height=600,scrollbars=no,toolbar=no,resizable=no,status=no');
	},
	
	OpenScreenViewer3: function(prod_id, cur, plat_id, specifics_plat_id, pubdate) {
		window.open('/ProductsScreenshotsViewer.aspx?id='+prod_id+'&cur='+cur+'&plat_id='+plat_id+'&specifics_plat_id='+specifics_plat_id+'&pubdate='+pubdate,'screenViwer','width=800,height=600,scrollbars=no,toolbar=no,resizable=no,status=no');
	},

	OpenScreenViewer4: function(prod_id, cur, plat_id, specifics_plat_id, pubdate) {
		window.open('/ProductsScreenshotsViewer.aspx?id='+prod_id+'&cur='+cur+'&plat_id='+plat_id+'&pubdate='+pubdate,'screenViwer','width=800,height=600,scrollbars=no,toolbar=no,resizable=no,status=no');
	},

	ShowPopup: function(id, width, height) {
	    window.open('ViewPopup.aspx?id='+id, 'mainPopup', 'width='+width+',height='+height+',toolbar=0,resizable=0,scrollbars=1');
	},
	
	GotoURL: function(id, url, target) {
		switch ( target )
		{
		    case 1:
			    document.location.href = 'Popup.Clicktrace.aspx?id=' + id + '&url=' + url;
			    window.close();
			    return false;
			    break;
		    case 2:
			    wWidth = opener.outerWidth || opener.document.body.clientWidth;
			    wHeight = opener.outerHeight || opener.document.body.clientHeight;
			    window.open('Popup.Clicktrace2.aspx?id=' + id + '&url=' + url, "extwin", "menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1,location=1,directories=1,personalbar=1,width="+wWidth+",height="+wHeight);
			    window.close();
			    return false;
			    break;
		    case 3:
			    opener.document.location.href = 'Popup.Clicktrace.aspx?id=' + id + '&url=' + url;
			    window.close();
			    return false;
			    break;
        }
        return true;
    },
    
    ShowUA: function(url, ev) {
        if ( !ev ) ev = window.event;
        ev.cancelBubble = true;
        if ( ev.stopPropagation ) ev.stopPropagation();
        window.open(url);
    }
}

var FormHandler = new Object();

FormHandler = {
    redirect: function(element) {
        if ( element.options[element.options.selectedIndex].value.indexOf('|')>0 )
        {
            var components = element.options[element.options.selectedIndex].value.split('|');
			if ( components[1]=="_blank" )
				window.open(components[0]);
			else
				document.location.href = components[0];
		}
        else
            document.location.href=element.options[element.options.selectedIndex].value;
    },
    
    validateCheckboxlist: function(source, arguments) {
        arguments.IsValid = false;
        var controltovalidate = p$(source.controltovalidate)
        if ( controltovalidate ) {
            var cboxes = controltovalidate.getElementsByTagName('input');
            for (i=0; i<cboxes.length; i++) {
                if ( cboxes[i].checked ) {
                    arguments.IsValid = true; 
                }
            }
        }
    },
    
    validateCheckbox: function(source, arguments) {
        arguments.IsValid = false;
        var controltovalidate = p$(source.controltovalidate)
        if ( controltovalidate ) {
            if ( controltovalidate.checked )
                arguments.IsValid = true;    
        }
    },
    
    pageValidate: function(event) {
		var el = Event.element(event);
		var pattern = /.*btnFormSubmit(\d+)/ig;
		var matches = pattern.exec( el.id );
		if ( !matches ) matches = pattern.exec( el.id ); /* fix for strange behaviour in FF */
		form_id = parseInt(matches[1]);
		Page_ClientValidate();
		if ( !Page_IsValid ) {
			var svSummary = p$( 'vsSummary' + form_id );
			if ( svSummary ) {
				Element.show(svSummary);
			}
			return false;
		} else {
			return true;
		}
    }
}


FormHandler.AutoResetField = Class.create();
FormHandler.AutoResetField.prototype = {
    inittxt:"",
    
    initialize: function(element, inittxt) {
        this.inittxt = inittxt;
        var field = p$(element);
        var eventArgs = new Object();
        eventArgs.value = this.inittxt;
        
        var fieldChangeHandler = function(e) {
            field.value='';
            Event.observe(field, 'blur', function(e) { if (field.value=='') field.value = this.value; }.bind(eventArgs), false);
        }
        
        Event.observe(field, 'focus', fieldChangeHandler, false);
        Event.observe(document.forms[0], 'submit', function() { if (field.value==this.value) field.value=''; }.bind(eventArgs), false);        
    }
}



var GUI = {
	FixGUI: function() {
		this.EAStoreLink();
		this.CreateScreenShotListStructure();
		this.FormClickSelectAll();
		//this.vlfix();
	},
	
	EAStoreLink : function() {
		var links = document.getElementsByClassName('iconEalinkAlt');
		links.each(function(item) {
			item.className += " fadedContent";
			item.target = "_blank";
			
			var spannode = createElement("span");
			var text = item.childNodes[0].cloneNode(true);
			spannode.appendChild(text);
			item.replaceChild(spannode, item.childNodes[0]);
			
			var wrapper = createElement("span");
			wrapper.className += " selfClear";
			Element.setStyle({display: "block"});
			wrapper.appendChild(item.cloneNode(true));
			item.parentNode.replaceChild(wrapper, item);
		});
	},
	
	CreateScreenShotListStructure: function() {
		var screens, parentEl, cscreen = null;
		var screenCont = document.getElementsByClassName('screenList'); /* extended product page */
		var const_rowitemcount = 7;
		if ( screenCont.length == 0 ) { /* basic product page */
			screenCont = document.getElementsByClassName('screens');
			const_rowitemcount = 4;
		}
		screenCont.each( function(item) {
			screens = item.getElementsByTagName('a');
			if ( screens.length > 0 ) {
				if ( parentEl==null ) parentEl = screens[0].parentNode;
				
				GUI.CreateRowStructure({
					rowItemCount: const_rowitemcount,
					containerElement: parentEl,
					rowItemCollection: $A(screens),
					rowElementClassNames: ' row selfClear'
				});
			}
			parentEl = null;
		});
	},
	
	CreateRowStructure: function( settings ) {
		settings = Object.extend( {
			rowItemCount: 3,
			rowElementType: 'div',
			rowElementClassNames: '',
			containerId: '',
			containerElement: null,
			rowItemClassName: 'item',
			rowItemCollection: null
		}, settings);
		
		var row;
		var rowitemcount = listlength = 0;
		
		if ( settings.containerElement == null )
			settings.containerElement = p$( settings.containerId );
			
		if ( settings.containerElement != null ) {
			if ( settings.rowItemCollection == null )
				settings.rowItemCollection = document.getElementsByClassName( settings.rowItemClassName, settings.containerElement );
			
			if ( settings.rowItemCollection.length > 0 ) {
				row = GUI.CreateRowStructureRow( settings.rowElementType, settings.rowElementClassNames );
				listlength = settings.rowItemCollection.length-1;
				for ( var i=0; i<=listlength; i++ ) {
					row.appendChild( settings.rowItemCollection[i] );
					
					rowitemcount++;
					if ( rowitemcount >= settings.rowItemCount ) {
						rowitemcount = 0;
						settings.containerElement.appendChild( row );
						row = GUI.CreateRowStructureRow( settings.rowElementType, settings.rowElementClassNames );
					}
				}
				if ( row.childNodes.length > 0 ) settings.containerElement.appendChild( row );
			}
		}
	},
	
	CreateRowStructureRow: function( elementType, classNames ) {
		var tmpEl = createElement(elementType);
		tmpEl.className += classNames;
		return tmpEl;
	},
	
	FormClickSelectAll: function() {
		var items = document.getElementsByClassName('selectAll');
		if ( items ) {
			for ( var i=items.length-1; i>=0; i-- ) {
				Event.observe( items[i], 'click', function(ev) { var el = Event.element(ev); el.focus(); el.select(); }, false);
			}
		}
	},
	
	vlfix: function() {
		var vl = p$('latestMovies');
		if ( vl ) {
			var items = document.getElementsByClassName('yt', vl);
			for ( i=0; i<items.length; i++ ) {
				var imgs = items[i].getElementsByTagName('img');
				imgs[2].src = '/Images/notfoundicon_youtubevideo.gif';
				imgs[2].width = 53;
				imgs[2].style.width = "53px";
				
				var icons = document.getElementsByClassName('icons', items[i]);
				icons[0].childNodes[0].style.display = 'none';
			}
		}
		
//		var np = p$('nowPlaying');
//		if ( np ) {
//			var imgs = np.getElementsByTagName('img');
//			imgs[0].src = '/Images/notfoundicon_youtubevideo.gif';
//			imgs[0].width = 53;
//			imgs[0].style.width = "53px";
//			
//			var b1 = document.getElementsByClassName('buttonSection', np);
//			if ( b1[0] ) b1[0].style.display = 'none';
//			b1 = document.getElementsByClassName('movieButtons', np);
//			if ( b1[0] ) b1[0].style.display = 'none';
//		}
	}
}
/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", GUI.FixGUI.bind(GUI), false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            GUI.FixGUI(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            GUI.FixGUI.bind(GUI); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
if (!document.addEventListener && (!(window.attachEvent && !window.opera)))
	window.onload = GUI.FixGUI.bind(GUI);



/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function openWin(theURL,winName,features) {
	win = window.open(theURL,winName,features);
	win.focus();
}

/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2001-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

function initScrollLayer() {
  var wndo = new dw_scrollObj('wn', 'lyr1');
  wndo.setUpScrollbar("dragBar", "track", "v", 1, 1);
  dw_scrollObj.GeckoTableBugFix('wn'); 
}

function initScrollLayer2() {
  var wndo = new dw_scrollObj('wn2', 'lyr2');
  wndo.setUpScrollbar("dragBar2", "track2", "v", 1, 1);
  //dw_scrollObj.GeckoTableBugFix('wn2'); 
}


function initScrollLayer3() {
  var wndo = new dw_scrollObj('wn3', 'lyr3');
  wndo.setUpScrollbar("dragBar3", "track3", "v", 1, 1);
  dw_scrollObj.GeckoTableBugFix('wn3'); 
}

/* Fansite scripts */
function toggleExpand( elnum ) {
	var thediv = p$( "cont_" + elnum );
	var thetext = p$( "text_" + elnum );
	var thebutton = p$( "button_" + elnum );
	// collapse or expand
	if( thediv && thebutton.innerHTML == "-" ) {
		thediv.innerHTML = thetext.value.substr( 0, 75 ) + "...";
		thebutton.innerHTML = "+";
		thebutton.className = "expandButton";
	} else {
		thediv.innerHTML = thetext.value;
		thebutton.innerHTML = "-";	
		thebutton.className = "expandButtonCollapse";
	}
	return false;
}
