window.onload = function() {
    Index.init();
}

var hider;
var lastInfoBtn;

var Index = {

    init: function() {
		if ($('success')){
		    Index.showSuccess();
		}else if ($('error')){
            Index.showError();
		}
		
		if(typeof(noResizeFlag) == "undefined") {
			// resize height of content-area if not enough content
			this.resizeContentHeight();
		}
    },

    resizeContentHeight: function() {
        var orgDim = $('main').getDimensions();

        var divID = !$('norm_contentRight') ? 'main' : 'norm_contentRight';

        if(orgDim.height < 770) {
            $(divID).style.height = '770px';
        }

        if($('norm_contentRight')) {
            var rDim = $('norm_contentRight').getDimensions();
            var lDim = $('norm_contentLeft').getDimensions();

            if(lDim.height > rDim.height) {
                // fix for the vertical line
                $('norm_contentRight').style.height = lDim.height + "px";
            }
        }
    },

	flushText: function(label, text){
		if($(label).value == text){
			$(label).value = '';
		}else if($(label).value == ''){
			$(label).value = text;
		}
	},

	showMenue: function(menueID,headerID){
	    var navPointPos = $(headerID).cumulativeOffset();
	    var navPointDim = $(headerID).getDimensions();
	    $(menueID).style.top = navPointPos.top + navPointDim.height + "px";
	    $(menueID).style.left = navPointPos.left + "px";
	    if ($(menueID).hasClassName('hidden')){
	        $(menueID).removeClassName('hidden');
	    }
	},

	hideMenueChooser: function(menueID) {
		if(!$(menueID).hasClassName('hidden')) {
			hider = window.setInterval('Index.startHideMenue(\''+menueID+'\')', 300);
		}

		return true;
	},

	HoverMyNav: function(img, node) {
		var chgNode = $(node);
		var img = "/skins/default/global/cpt_loggedin/" + img;
		chgNode.style.backgroundImage = "url(" + img + ")";
	},

	startHideMenue: function(menueID) {
		$(menueID).addClassName('hidden');
		window.clearInterval(hider);
	},

	stopHideMenue: function() {
		window.clearInterval(hider);
	},

	hideMenues: function() {
	   $$('.menueContainer').each(function(n) {
	      n.addClassName('hidden');
	   });
	},

	flipBgr: function(node, color) {
	   $(node).style.backgroundColor = color;
	},

	HoverLogReg: function(img, node) {
		var chgNode = $(node);
		var img = "/skins/default/global/" + img;
		chgNode.style.backgroundImage = "url(" + img + ")";
	},

	removeBgr: function() {
	   $$('.menue').each(function(n) {
	      n.style.backgroundColor = '';
	   });
	},

	//InfoBox
	handleInfoBox: function(infoID,custOpt) {
	   if(infoID != lastInfoBtn) {
	   	$('infoBox').addClassName('hidden');
	   	if($(lastInfoBtn+'InfoBtn')) {
	   		if(typeof(custOpt)!="undefined") {
	   			$(lastInfoBtn + custOpt +'InfoBtn').style.zIndex = 50;
	   		} else {
	   			$(lastInfoBtn+'InfoBtn').style.zIndex = 50;
	   		}
	   	}
	   }	  
	   if ($('infoBox').hasClassName('hidden')){
	       $('infoBox').removeClassName('hidden');
	       if(typeof(custOpt)!="undefined") {
	       		var pos = $(infoID + custOpt + 'InfoBtn').cumulativeOffset();	       		
	       		$(infoID+ custOpt+'InfoBtn').style.zIndex = 999999;			   		
			} else {
				var pos = $(infoID + 'InfoBtn').cumulativeOffset();
				$(infoID+'InfoBtn').style.zIndex = 999999;				    
			}
		   
		   $('infoBox').style.zIndex = 999999;
			
    	   $('infoBox').style.left = pos.left + "px";
    	   $('infoBox').style.top = pos.top + "px";
    	   $('infoBoxHeader').innerHTML = $(infoID + 'InfoHeader').innerHTML;
    	   $('infoBoxBody').innerHTML = $(infoID + 'InfoText').innerHTML;
    	   lastInfoBtn = infoID;
	   }else{
	       $('infoBox').addClassName('hidden');
	        if(typeof(custOpt)!="undefined") {	        	
	        	$(infoID+ custOpt+'InfoBtn').style.zIndex = 50;
	        } else {
	       		$(infoID+'InfoBtn').style.zIndex = 50;
	        }
	   }
	},


	//InfoLayer

	showSuccess: function() {
        $('infoLayer_headline').innerHTML = $('success_headline').innerHTML;
        $('infoText').innerHTML = $('success_content').innerHTML;
	    Index.showInfoLayer();
	},

	showError: function() {
        $('infoLayer_headline').innerHTML = $('error_headline').innerHTML;
        $('infoText').innerHTML = $('error_content').innerHTML;
        Index.showInfoLayer();
	},

	switchGameOffline: function() {
        $('infoLayer_headline').innerHTML = $('offlineGame_headline').innerHTML;
        $('infoText').innerHTML = $('offlineGame_content').innerHTML;
        Index.showInfoLayer();
	},
	
	sendGameToQa: function() {
        $('infoLayer_headline').innerHTML = $('sendGameToQa_headline').innerHTML;
        $('infoText').innerHTML = $('sendGameToQa_content').innerHTML;
        Index.showInfoLayer();
	},

	showforgetPWD: function() {
        $('infoLayer_headline').innerHTML = $('forgetPWD_headline').innerHTML;
        $('infoText').innerHTML = $('forgetPWD_text').innerHTML;
	    Index.showInfoLayer();
	    Index.resizeInfoLayer();
	    Index.repositionInfoLayer();
	},

	setGameOffline: function() {
        if ($('gameOffline').value == 1){
            $('gameOffline').value = 0;
        }else{
            $('gameOffline').value = 1;
        }
	},
	
	setSendGameToQa: function(sendToQaChkObj) {		
        if (sendToQaChkObj.checked){
            $('sendIt').value = 1;
        }else{
            $('sendIt').value = 0;
        }
	},

	closeInfoLayer: function() {
        if (!$('infoLayer').hasClassName('hidden')) $('infoLayer').addClassName('hidden');
	},

	showInfoLayer: function() {
        if ($('infoLayer').hasClassName('hidden')) $('infoLayer').removeClassName('hidden');
        new Draggable('infoLayer',{handle: 'infoLayer_top'});
	    Index.resizeInfoLayer();
	    Index.repositionInfoLayer();
	},

	resizeInfoLayer: function() {
	   var infoTextDim = $('infoText').getDimensions();
	   $('infoLayer_left').style.height = infoTextDim.height + "px";
	   $('infoLayer_right').style.height = infoTextDim.height + "px";
	},

	repositionInfoLayer: function() {
        var dimLayer = $('infoLayer').getDimensions();
        var dimDoc = document.viewport.getDimensions();
        var dimScroll = document.viewport.getScrollOffsets();
        $('infoLayer').style.top = dimScroll.top + dimLayer.height/2 - 20 + "px";
        $('infoLayer').style.left = dimDoc.width/2 - dimLayer.width/2 + "px";
	},

	changeSelect: function (titleSelect, titleSpan){
        //var x = $F($(titleSelect));
        //$(titleSpan).update($(x).innerHTML);
        var x = $(titleSelect).value;
        $(titleSpan).update($(titleSpan+'Option_'+x).innerHTML);
	},

	changeSetCountry: function (titleSelect, countrySpan){
	    var x = $('countrySignUp').value;
        $('countrySpan').update($('country_'+x).innerHTML);
	},

	copyLang2Right: function (selectFrom, selectTo){
	    var removeIDs = new Array();
	    var j = 0;

	    // run through options
	    for(var i = 0; i < $(selectFrom).options.length; i++) {
	        if($(selectFrom).options[i].selected == false) continue;

	        // clone current options
	        var opt = $(selectFrom).options[i].cloneNode(true);
	        opt.id = $(selectFrom).options[i].value;

	        $(selectTo).appendChild(opt);
	        $(opt.id).addClassName("projectLanguage");

	        // remove current options from selectFrom list
	        removeIDs[j] = $(selectFrom).options[i].id;
	        j++;
	    }

	    // remove selected options
	    for(var i = 0; i < removeIDs.length; i++) { $(removeIDs[i]).remove(); }
	},

	copyLang2Left: function (selectFrom, selectTo){
        var removeIDs = new Array();
	    var j = 0;

	    // run through options
	    for(var i = 0; i < $(selectFrom).options.length; i++) {
	        if($(selectFrom).options[i].selected == false) continue;

	        // clone current options
	        var opt = $(selectFrom).options[i].cloneNode(true);
	        opt.id = $(selectFrom).options[i].value + "_list";

	        $(selectTo).appendChild(opt);
	        $(opt.id).removeClassName("projectLanguage");

	        // remove current options from selectFrom list
	        removeIDs[j] = $(selectFrom).options[i].id;
	        j++;
	    }

	    // remove selected options
	    for(var i = 0; i < removeIDs.length; i++) { $(removeIDs[i]).remove(); }
	},

	//Global Functions

	resendPassword: function () {
	    var loginData;
	    $$('.loginData').each(function(n) { if (!$(n).hasClassName('hidden') && ($(n).value.length<20 || $(n).value.indexOf('@') != 1)) loginData = $(n).value; });
		$$('.neededData').each(function(n) { if (!$(n).hasClassName('hidden')) $(n).addClassName('hidden'); });
		$$('.sendLoginData').each(function(n) { if (!$(n).hasClassName('hidden')) $(n).addClassName('hidden'); });
		$$('.waiting4Answer').each(function(n) { if ($(n).hasClassName('hidden')) $(n).removeClassName('hidden'); });
		Index.resizeInfoLayer();
		new Ajax.Request("/ajax/resendPassword.php", {
	    	postBody: "loginData="+loginData,
	    	onSuccess: function(r) {
	    	    var obj = r.responseText.evalJSON();
	    		var success = obj.text;
                    $$('.waiting4Answer').each(function(n) { if (!$(n).hasClassName('hidden')) $(n).addClassName('hidden'); });
                    $$('.neededData').each(function(n) { if ($(n).hasClassName('hidden')) $(n).removeClassName('hidden'); });

	    		    if (obj.result == 'success'){
	    		        $$('.forgetPWD_error').each(function(n) { if (!$(n).hasClassName('hidden')) $(n).addClassName('hidden'); });
                        $$('.neededData').each(function(n) { if (!$(n).hasClassName('bold')) $(n).addClassName('bold'); });
                        $$('.neededData').each(function(n) { if (!$(n).hasClassName('successGreen')) $(n).addClassName('successGreen'); });
                        $$('.neededData').each(function(n) { n.style.marginLeft = 10 + "px" });
                        $$('.neededData').each(function(n) { n.update(success) });
    	    			$('sendLoginData').remove();
	    		    }else{
                        $$('.forgetPWD_error').each(function(n) {
                            n.innerHTML = success;
                            n.style.marginBottom = 5 + "px";
                            if ($(n).hasClassName('hidden')) $(n).removeClassName('hidden');
                            $$('.loginData').each(function(m) { if (!$(m).hasClassName('input_error')) $(m).addClassName('input_error'); });
                            $$('.sendLoginData').each(function(n) { if ($(n).hasClassName('hidden')) $(n).removeClassName('hidden'); });
                         });
	    		    }
                    Index.resizeInfoLayer();
	    	}
	    });
	},
	
	resizeIframe: function (iframeID){
	    var cookieHandling;
	    cookieHandling = window.setInterval('Index.checkPaymentHeightByCookie(\''+ iframeID +'\')', 100);
	},
	
	checkPaymentHeightByCookie: function (iframeID){
	    var heightFrame = this.getCookie('Payment_IFrameSize');
	    if (heightFrame > 600){
	       $('norm_contentRight').style.height = '';
	       $(iframeID).style.height = parseInt(heightFrame) + 50 + 'px';
	    }
	},
	
	wordCount: function (inputID, maxLength){
        var data = $(inputID).value;
        var extract = data.split(" ");
        if(extract.length > maxLength){
            var newText = '';
            for(var i = 0; i <= maxLength; i++){
                newText += extract[i] + " ";
            }
            $(inputID). value = newText;
            $(inputID).scrollTop = $(inputID).scrollHeight; 
        }
        
	},
	
	charCount: function (inputID, maxLength){
        var data = $(inputID).value;
        if(data.length > maxLength){
            $(inputID).value = $(inputID).value.substring(0, maxLength);
            $(inputID).scrollTop = $(inputID).scrollHeight; 
        }
        
	},
	
	getCookie: function (check_name){
    	// first we'll split this cookie up into name/value pairs
    	// note: document.cookie only returns name=value, not the other components
    	var a_all_cookies = document.cookie.split( ';' );
    	var a_temp_cookie = '';
    	var cookie_name = '';
    	var cookie_value = '';
    	var b_cookie_found = false; // set boolean t/f default f
    	
    	for ( i = 0; i < a_all_cookies.length; i++ )
    	{
    		// now we'll split apart each name=value pair
    		a_temp_cookie = a_all_cookies[i].split( '=' );				
    		// and trim left/right whitespace while we're at it
    		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');	
    		// if the extracted name matches passed check_name
    		if ( cookie_name == check_name )
    		{
    			b_cookie_found = true;
    			// we need to handle case where cookie has no value but exists (no = sign, that is):
    			if ( a_temp_cookie.length > 1 )
    			{
    				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
    			}
    			// note that in cases where cookie is initialized but no value, null is returned
    			return cookie_value;
    			break;
    		}
    		a_temp_cookie = null;
    		cookie_name = '';
    	}
    	if ( !b_cookie_found ) 
    	{
    		return null;
    	}
    },

	getLanguageText: function (path,element,mode) {
		new Ajax.Request("/ajax/getLanguageText.php", {
	    	postBody: "path="+path,
	    	onSuccess: function(r) {
	    		var success = r.responseText;
	    			if(mode=='value'){
	    				$(element).value= success;
	    			}else if(mode=='div'){
	    				$(element).update(success);
	    			}
	    	}
	    });
	},


	setLanguageSelect: function (titleSelect, titleSpan){
        var x = $(titleSelect).value;
        $(titleSpan).update($(titleSpan+'Option_'+x).innerHTML);
	},

	//Projectmanagement

	setActiveSubPoint: function (ID) {
	    var splitter = 0;
	    var splitter2;
	    //hide all projectSubContainer set arrows back
	     $$('.projectSubHider').each(function(n){ if (!n.hasClassName('hidden')) n.addClassName('hidden'); });
	     //remove class arrowblackDown or backgroundimage via style
	     $$('.arrowblackTop').each(function(n){
             n.style.backgroundImage = "";
	     });
	     $$('.arrowblackDown').each(function(n){
	         splitter2 = n.id.split('_');
	         if(splitter2[0] == 'projectTextInner'){
                 n.removeClassName('arrowblackDown');
                 n.addClassName('arrowblackTop');
	         }
	     });

 	     $$('.activeProject').each(function(n){
             n.removeClassName('activeProject');
             n.removeClassName('arrowblackDown');
             n.addClassName('arrowblackTop');
             splitter = n.id.split('_');
	     });

	     //set all container to arrowTop
	    if($('projectSubContainer_'+ID).hasClassName('hidden') && splitter[1] != ID){
    		var img = "/skins/default/global/up_arrow_down.gif";
    		$('projectTextInner_'+ID).style.backgroundImage = "url(" + img + ")";
    		$('projectTextInner_'+ID).addClassName('activeProject');
    		$('projectSubContainer_'+ID).removeClassName('hidden');
	    }else{
	       var img = "/skins/default/global/up_arrow_333333.gif";
    		$('projectTextInner_'+ID).style.backgroundImage = "url(" + img + ")";
    		$('projectSubContainer_'+ID).addClassName('hidden');
    		$('projectSubContainer_'+ID).addClassName('activeProjectSubContainer');
	    }
	},
	setActivePaymentSubPoint: function (ID) {
	   var splitter = 0;
	   $$('.activePayment').each(function(n){
             n.removeClassName('activePayment');
             n.removeClassName('arrowblackDown');
             n.addClassName('arrowblackTop');
             splitter = n.id.split('_');
	     });

	   if($('projectPaymentContainer_'+ID).hasClassName('hidden')){
	       $('projectPaymentContainer_'+ID).removeClassName('hidden');
	       var img = "/skins/default/global/up_arrow_down.gif";
    	   $('paymentPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }else{
   	       $('projectPaymentContainer_'+ID).addClassName('hidden');
	       var img = "/skins/default/global/up_arrow_333333.gif";
    	   $('paymentPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }
	},

	setActiveStatusSubPoint: function (ID) {
	   var splitter = 0;
	   $$('.activeStatus').each(function(n){
             n.removeClassName('activeStatus');
             n.removeClassName('arrowblackDown');
             n.addClassName('arrowblackTop');
             splitter = n.id.split('_');
	     });

	   if($('projectStatusContainer_'+ID).hasClassName('hidden')){
	       $('projectStatusContainer_'+ID).removeClassName('hidden');
	       var img = "/skins/default/global/up_arrow_down.gif";
    	   $('statusPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }else{
   	       $('projectStatusContainer_'+ID).addClassName('hidden');
	       var img = "/skins/default/global/up_arrow_333333.gif";
    	   $('statusPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }
	},
	
	setActiveNewsSubPoint: function (ID) {
	   var splitter = 0;
	   $$('.activeStatus').each(function(n){
             n.removeClassName('activeStatus');
             n.removeClassName('arrowblackDown');
             n.addClassName('arrowblackTop');
             splitter = n.id.split('_');
	     });

	   if($('projectNewsContainer_'+ID).hasClassName('hidden')){
	       $('projectNewsContainer_'+ID).removeClassName('hidden');
	       var img = "/skins/default/global/up_arrow_down.gif";
    	   $('statusNewsPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }else{
   	       $('projectNewsContainer_'+ID).addClassName('hidden');
	       var img = "/skins/default/global/up_arrow_333333.gif";
    	   $('statusNewsPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }
	},

	setActiveTestcenterSubPoint: function (ID) {
	   var splitter = 0;
	   $$('.activeTestcenter').each(function(n){
             n.removeClassName('activeTestcenter');
             n.removeClassName('arrowblackDown');
             n.addClassName('arrowblackTop');
             splitter = n.id.split('_');
	     });
	   if($('projectTestcenterContainer_'+ID).hasClassName('hidden')){
	       $('projectTestcenterContainer_'+ID).removeClassName('hidden');
	       var img = "/skins/default/global/up_arrow_down.gif";
    	   $('testcenterPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }else{
	        $('projectTestcenterContainer_'+ID).addClassName('hidden');
	       var img = "/skins/default/global/up_arrow_333333.gif";
    	   $('testcenterPoint_'+ID).style.backgroundImage = "url(" + img + ")";
	   }
	},

	chkGenres: function(chkID){
	    var count = 0;
        $$('input[name="chk_genre[]"]').each(function(n){
            if (n.checked){
                count++;
            }
        });
        if(count > 3){
            $('chk_' + chkID).checked = false;
        }
	},

	//Marketing
	reloadMarketing: function (gameID, lang) {
	   if(lang != '-'){
	       location.href = '/projects/marketing/' + gameID + '/' + lang + '/';
	   }
	},


	//Support
	reloadSupport: function (gameID, lang) {
	   if(lang != '-'){
	       location.href = '/projects/support/' + gameID + '/' + lang + '/';
	   }
	},

	//Marketing
	checkMarketing: function () {
	   if($('languageSelect').value != '' && $('languageSelect').value != '-'){
	       document.marketingForm.submit.click();
	   }
	},

	//Support
	checkSupport: function () {
	   if($('languageSelect').value != '' && $('languageSelect').value != '-'){
	       document.supportForm.submit.click();
	   }
	},

	//SEO
	checkSEO: function () {
	   if($('languageSelect').value != '' && $('languageSelect').value != '-'){
	       document.seoForm.submit.click();
	   }
	},

	reloadSEO: function (gameID, lang) {
	   if(lang != '-'){
	       location.href = '/projects/seo/' + gameID + '/' + lang + '/';
	   }
	},

	//EditProject
	checkEditProject: function () {
        $$('.projectLanguage').each(function(n) {
            n.setAttribute("selected", "selected");
        });
        document.editProjectForm.submit.click();
	},

	//AddNewProject
	addNewProject: function () {
        $$('.projectLanguage').each(function(n) {
            n.setAttribute("selected", "selected");
        });
        document.addNewProjectForm.submit.click();
	},

	//Status
	handleStatus: function (menueID, statusID) {
	   if($(statusID).hasClassName('hidden')){
	       $(statusID).removeClassName('hidden');
	       $(menueID).removeClassName('statusMenueArrow');
	       $(menueID).addClassName('statusMenueArrowDown');
	   }else{
           $(statusID).addClassName('hidden');
           $(menueID).addClassName('statusMenueArrow');
           $(menueID).removeClassName('statusMenueArrowDown');
	   }
	},

	checkURL: function(imgID, url) {
        new Ajax.Request("/ajax/checkURL.php", {
            postBody: "url=" + url,

            onSuccess: function(r) {
                if(r.responseText == 'OK') {
                    $(imgID).src = "/skins/default/global/ok.jpg";
                }
                else {
                    $(imgID).src = "/skins/default/global/error.jpg";
                }
            }
        })
	},
	
	showContainer: function(containerID) {
		$(containerID).removeClassName('hidden');		
	}
}