/*Main.js on*/
function fCheckEMail(s)
{
	var a = false;
	var res = false;
	if(typeof(RegExp) == 'function')
	{
		var b = new RegExp('abc');
  		if(b.test('abc') == true)
			a = true;
  	}
 	if(a == true)
 	{
  		reg = new RegExp('^([a-zA-Z0-9-._]+)'+
                   '(@)([a-zA-Z0-9-.]+)'+
                   '(.)([a-zA-Z]{2,4})$');
  		res = (reg.test(s));
 	}
 	else
 	{
  		res = ( s.search('@') >= 1 && s.lastIndexOf('.') > s.search('@') &&  s.lastIndexOf('.') >= s.length-5 );
 	}
	return( res );
}

function str2int (strTemp)
{
	if (strTemp.indexOf("px")  != -1 )
		strTemp = strTemp.slice(0,strTemp.indexOf("px"));
	if (strTemp.indexOf("%")  != -1 )
		strTemp = strTemp.slice(0,strTemp.indexOf("%"));
		
	strTemp++; 
	strTemp--;
	
	return strTemp;
}

function fncPreImgs(){
	if (document.images)
	{
		this.objNorImg		= fncPreImgs.arguments[0];	
		this.objPreImg		= new Array();
		for (var z=1; this.objNorImg.length ;z++)
		{
			if (!this.objNorImg[z])	break;
			this.objPreImg[-z]		= new Image();
			this.objPreImg[-z].src	= this.objNorImg[z];
			this.objPreImg[z]		= new Image();
			this.objPreImg[z].src	= this.objNorImg[z].slice(0,(this.objNorImg[z].length-4))
			+"_m"
			+this.objNorImg[z].slice((this.objNorImg[z].length-4),this.objNorImg[z].length);
			//document.write("Preload Image "+z+" pfad: "+ this.objPreImg[z].src+"<br>");
			//document.write("Preload Image -"+z+" pfad: "+ this.objPreImg[-z].src+"<br>");
		}
		this.preloadFlag = true;
	}
}

function changeImages() {
	if (objIMG.preloadFlag) {
		var result;
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			if (!isNaN(changeImages.arguments[i+1])){
				result = objIMG.objPreImg[-(changeImages.arguments[i+1])].src;
			}else if(changeImages.arguments[i+1].length <= 4) {
				result = objIMG.objPreImg[parseInt(changeImages.arguments[i+1])].src;
			}else{
				result = changeImages.arguments[i+1];
			}
			document[changeImages.arguments[i]].src = result;
		}
	}
}

function getObjXPos(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	obj = null;
	return curleft;
}


function getObjYPos(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	obj = null;
	return curtop;
}

var intInnerWidth = 0;
var intInnerHeight = 0;
function recalculateInnerDimension()
{
	intInnerWidth = document.body.clientWidth;
	intInnerHeight = document.body.clientHeight;
	return false;
}

function centerDivWindow(strDivId, intTopOffset, intLeftOffset, strMode)
{
	if(document.getElementById(strDivId) == null)
		return;

	var objDiv = document.getElementById(strDivId);
	var boolTop = true;
	var boolLeft = true;
	if(strMode == "horizontal")
		boolTop = false;
	if(strMode == "vertical")
		boolLeft = false;
	
	if(boolLeft == true){
		var nLeft = objDiv.offsetWidth;
		nLeft = intInnerWidth/2 - nLeft/2;
		nLeft = nLeft - 15 + document.body.scrollLeft - intLeftOffset;
		objDiv.style.left = nLeft + "px";
	}
	if(boolTop == true){
		var nTop = objDiv.offsetHeight;
		nTop = intInnerHeight/2 - nTop/2;
		nTop = nTop - 15 + document.body.scrollTop - intTopOffset;
		objDiv.style.top = nTop + "px";
	}
	objDiv = null;
}

var boolIsIE6 = false;
var arrImage = new Array();

function fSetOpacity(obj, svalue) {
	obj.style.opacity = svalue/100;
	obj.style.filter = 'alpha(opacity=' + svalue + ')';
}

function openInNewBrowserWindow(strUrl){
	var win = window.open(strUrl,  'NewWindow');
}

function handleWindowOnLoad(){
	recalculateInnerDimension();
	if(boolIsIE6 == true)
		calculateBackGround();
	if(document.getElementById('oDivVotePreviewContainer') != null)
		centerDivWindow('oDivVotePreviewContainer', 0, 0, 'horizontal');
	if(document.getElementById('oDivMusicContainer') != null)
		centerDivWindow('oDivMusicContainer', 0, 0, 'horizontal');
	
}

function handleResizeWindow(){
	recalculateInnerDimension();
	if(boolIsIE6 == true)
		calculateBackGround();
	if(document.getElementById('oDivVotePreviewContainer') != null)
		centerDivWindow('oDivVotePreviewContainer', 0, 0, 'horizontal');
	if(document.getElementById('oDivPreviewContainer') != null)
		centerDivWindow('oDivPreviewContainer', 0, 0, '');
	if(document.getElementById('oDivMusicContainer') != null)
		centerDivWindow('oDivMusicContainer', 0, 0, 'horizontal');
}
/*Main.js off*/

/*PopUp.js on*/
function fCenterWindow(intWidth,intHeight)
{
   var strOutput = "";

	// Middle Point Of Screen
   intWidth  = (screen.availWidth / 2) - (intWidth / 2) ;
   intHeight = ((screen.availHeight-20) / 2) - (intHeight / 2) ;
   
   // Netscape 4.7
   strOutput += ",screenx=" + intWidth;
   strOutput += ",screeny=" + intHeight;
   
   // Internet Explorer 4.0
   strOutput += ",left=" 	+ intWidth ;
   strOutput += ",top=" 	+ intHeight;
 
   return strOutput;

}

function fLeftWindow(intHeight)
{
   var strOutput = "";

   intHeight = screen.availHeight - (20 + intHeight) ;
   
   // Netscape 4.7
   strOutput += ",screenx=0" ;
   strOutput += ",screeny=" + intHeight;
   
   // Internet Explorer 4.0
   strOutput += ",left=0";
   strOutput += ",top=" 	+ intHeight;
 
   return strOutput;

}

function fOpenHtmlWindow(strURL, strName, nWidth, nHeight, nScreenX, nScreenY)
{
	//SecondFenster();
   	var win = window.open(strURL, strName,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+nWidth+",height="+nHeight+", screenx=" + nScreenX + ", screeny=" + nScreenY + " ");
	win.focus();
}

function fOpenCenteredWindow(strURL, nWidth, nHeight)
{
	//SecondFenster();
	var sScroll = "auto";
	if(fOpenCenteredWindow.arguments[3] != null){
		sScroll = fOpenCenteredWindow.arguments[3];
	}
	var str = fCenterWindow(nWidth,nHeight);
   	var win = window.open(strURL, "CenteredWindow","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars="+sScroll+",resizable=yes, width="+nWidth+", height="+nHeight+" " + str + " ");
	win.focus();
}
/*PopUp.js off*/

function errorHandler(error, script, lineNumber) {
    $.ajax({
        url: '/onjavascripterror',
        data: {
            Error: error,
            Script: script,
            LineNumber: lineNumber
        },
        error: function () { }
    });
    
    return true;
}

function ajaxErrorHandler(event, XMLHttpRequest, ajaxOptions, thrownError) {
    //debugger;
    var errorData = "";
    
    $.ajax({
        url: '/onajaxerror',
        data: {
            ErrorData: errorData
        },
        error: function () { }
    });
}

window.onerror = errorHandler;

$(document).ready(
    function () {
        $("body").ajaxError(ajaxErrorHandler);
    }
);

function ShowErrorField()
{
	$('input:text.input-validation-error, input:password.input-validation-error, select.input-validation-error, textarea.input-validation-error').parent().addClass('error');
	$('input:radio.input-validation-error').parent().parent().parent().parent().parent().addClass('error');
}

function initialize() {
    $('body').addClass('js');
    ShowErrorField();
    if ($(':checkbox, :radio').length) $(':checkbox, :radio').checkBox();
    if ($('.scroll-pane').length) $('.scroll-pane').jScrollPane();
    $('.select').uniform();
    if ($('a.lightbox_gallery').length) $('a.lightbox_gallery').lightBox();
    if ($('a.lightbox_ajax').length) $('a.lightbox_ajax').lightBox();
    $("form").bind("submit", function () { $.watermark.hideAll(); });

    $("body").ajaxStart(function () { $(".loadingbox").show(); });
    $("body").ajaxComplete(function () { $(".loadingbox").hide(); });
}

$(document).ready(function () {
    initialize();
});

(function($) {
    $.fn.serializeAnything = function() {
        var toReturn = [];
        var els = $(this).find(':input').get();
        $.each(els, function() {
            if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
                var val = $(this).val();
                toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
            }
        });   

        return toReturn.join("&").replace(/%20/g, "+");
    }

})(jQuery);