$(document).ready(
    function()
    {
        //errorBox
        $("#errorMsg #err_close").click(function () {
            $("#errorMsg").hide();
        });
        
        
        $("a.tooltip").ToolTipDemo();
        
        $("#list-table tbody tr.tr_hover:odd").addClass('odd');
        $("#list-table tbody tr.tr_hover:even").addClass('even');
        
        $("#list-table tbody tr.tr_hover").hover(function () {
            $(this).toggleClass("hover");
        }, function () {
            $(this).toggleClass("hover");
        });
        
        //tableRow styling
        $("#list-table tbody tr.tr_hover:odd").addClass('odd');
        $("#list-table tbody tr.tr_hover:even").addClass('even');
        
        $("#list-table tbody tr.tr_hover").hover(function () {
            $(this).toggleClass("hover");
        }, function () {
            $(this).toggleClass("hover");
        });
        
        //active latest item
        if($(".lb_show").attr("id"))
        {
            showLbContent($(".lb_show").attr("id"), 850 );
        }
        
        //abort links
        $(".abort").click(popupLbClose);
        
        //onload wird zweitfenster geschlossen
        //salespersonview
        
        //alert(window.name);
        if((window.name!='secondview')&&(window.name=='popup_on'))
        {
            window.open('', 'secondview').close();
            window.name = '';
        }
        
        //init search_field_value
        aboSearchFormClass = $("#abandonment_search_form").attr( 'class' );
        $("#field_search_value").html($("#"+aboSearchFormClass).html());
        
        
        //confirm
        /*
        $('a.del').confirm({
            msg:'See my interesting offer?',
            timeout:5000,
            dialogShow:'fadeIn',
            dialogSpeed:'slow',
            buttons: {
                ok:'Sure',
                cancel:'No thanks',
                separator:'***',
                wrapper:'<button></button>'
            }
        });
        */
    }
);

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------//

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------


function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}



function toggleDisplayDetails(toggleID)
{
    tr_active = $("#tr_active_"+toggleID);
    div_toggle = $("#div_toggle_"+toggleID);
    
    if(div_toggle.hasClass("toggle_active"))
    {
        div_toggle.removeClass("toggle_active");
        div_toggle.addClass("toggle");
        div_toggle.slideToggle("normal");
        
        tr_active.removeClass("tr_active");
    }
    else if($(".toggle_active").html())
    {
        $(".toggle_active").each(function(){
            $(this).removeClass("toggle_active");
            $(this).addClass("toggle");
            $(this).slideUp("normal");
            
            $("#list-table .tr_active").removeClass("tr_active");
        });
        
        div_toggle.removeClass("toggle");
        div_toggle.addClass("toggle_active");
        div_toggle.slideToggle("normal");
        
        tr_active.addClass("tr_active");
    }
    else
    {
        div_toggle.removeClass("toggle");
        div_toggle.addClass("toggle_active");
        div_toggle.slideToggle("normal");
        
        tr_active.addClass("tr_active");
    }
}


function showListDetails()
{
	$(this).blur();
	tmpButton = $(this);
	tmpTr = $(this).parent("td").parent("tr");
	
	if($("#list-table").attr("class")=='user')
	{
		ajaxData = '&act=detailUser';
	} else ajaxData = '&act=detailContract';
	
	tmpCloseTr = $("#list-table tr.active_toggle_box");
	
	if(tmpCloseTr.attr("class")=='active_toggle_box')
	{
		//delete details
		$("#list-table tr.active_toggle_box .toggle").slideToggle("normal", function(){
			tmpCloseTr.remove();
			$("#list-table tr.active").removeClass("active");
			
			//ajax
			tmpTr.addClass("active");
			$.ajax({
				data: ajaxData,
				url: "ajax_detail.php",
				type: "GET",
				cache: false,
				beforeSend: function(){
					$("#loading div").attr("style","background-image:url(img/ajax-loader.gif);");
			    $("#loading").attr("style","display:block;");
			   },
				success: function(html){
					tmpTr.after(html);
					
					$("#loading").attr("style","display:none;");
					
					$("#list-table tr.active_toggle_box .toggle").slideToggle("normal");
					tmpButton.unbind('click', showListDetails);
					tmpButton.click(hideListDetails);
				}
			});
			
		});
		
		$("#list-table tr.active a.bu_toggle").unbind('click', hideListDetails);
		$("#list-table tr.active a.bu_toggle").click(showListDetails);
		
	}
	else
	{
		//ajax
		tmpTr.addClass("active");
		$.ajax({
			data: ajaxData,
			url: "ajax_detail.php",
			type: "GET",
			cache: false,
			beforeSend: function(){
				$("#loading div").attr("style","background-image:url(img/ajax-loader.gif);");
		    $("#loading").attr("style","display:block;");
		   },
			success: function(html){
				tmpTr.after(html);
				
				$("#loading").attr("style","display:none;");
				
				$("#list-table tr.active_toggle_box .toggle").slideToggle("normal");
				tmpButton.unbind('click', showListDetails);
				tmpButton.click(hideListDetails);
			}
		});
	}
	
}

function hideListDetails()
{
	$(this).blur();
	$("#list-table tr.active_toggle_box .toggle").slideToggle("normal", function(){
		$("#list-table tr.active_toggle_box").remove();
		$("#list-table tr.active").removeClass("active");
	});
	
	$("#list-table tr.active a.bu_toggle").unbind('click', hideListDetails);
	$("#list-table tr.active a.bu_toggle").click(showListDetails);
}


function popup( url, pWidth, pHeight , param )
{
		var opac=0.7;
	 
		//overlay
		var arrayPageSize = getPageSize();
		
		// calculate top and left offset for the lightbox 
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = (pWidth > arrayPageSize[0]) ? 0 : ((arrayPageSize[0]-pWidth)/2)+arrayPageScroll[0];
		
		var overlayWidth = ((pWidth+20+lightboxLeft) > arrayPageSize[0]) ? pWidth+20+lightboxLeft  : arrayPageSize[0] ;
		var overlayHeight = ((pHeight+20+lightboxTop) > arrayPageSize[1]) ? pHeight+20+lightboxTop  : arrayPageSize[1] ;
		
		//overlay values
		var styleOverlayValue = 'width:'+overlayWidth+'px; height:'+overlayHeight+'px;';
		
		//lightbox values
		var styleLightboxValue = 'width:'+pWidth+'px; height:'+pHeight+'px; top:'+lightboxTop+'px; left:'+lightboxLeft+'px;';
		
		//start overlay
		
		//for ie6 bug 
		$("select").attr("style","visibility:hidden;");
		
		$("#overlay").attr("style",styleOverlayValue);
		$("#overlay").fadeTo("fast", opac);

			 
			
		//ajax
		$.ajax({
			data: param,
			url: url,
			type: "GET",
			cache: false,
			beforeSend: function(){
				$("#loading div").attr("style","background-image:url(img/ajax-loader-popup.gif);");
		    $("#loading").attr("style","display:block;");
		   },
			success: function(html){
				$("#lightbox #lb_content").html(html);
				
				$("#loading").attr("style","display:none;");
				
				$("#lightbox").attr("style",styleLightboxValue);
				$("#lightbox").slideDown("normal");
				
				$("#lightbox #lb_close").click(popupClose);
			}
		});
		
}

function popupClose()
{
	//for ie6 bug
	$("select").attr("style","visibility:show;");
	$("#lightbox").fadeOut("fast");
	$("#overlay").fadeOut("fast");
}


//new  

function showLbContent(showID, pWidth, pHeight)
{
    	var opac=0.7;
        
        pWidth = pWidth ? pWidth : $("#"+showID).width();
        pHeight = pHeight ? pHeight : $("#"+showID).height(); 
        
        if($.browser.msie) pHeight = 0.92 * pHeight ;
        
        //alert(pWidth+' x '+pHeight);
        
		//overlay
		var arrayPageSize = getPageSize();
		
		// calculate top and left offset for the lightbox 
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		var lightboxLeft = (pWidth > arrayPageSize[0]) ? 0 : ((arrayPageSize[0]-pWidth)/2)+arrayPageScroll[0];
		
		var overlayWidth = ((pWidth+20+lightboxLeft) > arrayPageSize[0]) ? pWidth+20+lightboxLeft  : arrayPageSize[0] ;
		var overlayHeight = ((pHeight+20+lightboxTop) > arrayPageSize[1]) ? pHeight+20+lightboxTop  : arrayPageSize[1] ;
		
		//overlay values
		var styleOverlayValue = 'width:'+overlayWidth+'px; height:'+overlayHeight+'px;';
		
		//lightbox values
		var styleLightboxValue = 'width:'+pWidth+'px; height:'+pHeight+'px; top:'+lightboxTop+'px; left:'+lightboxLeft+'px;';
		
		//start overlay
		
		//for ie6 bug 
		$("select").attr("style","visibility:hidden;");
        $("#"+showID+" select").attr("style","visibility:visible;");
		
		$("#form_overlay").attr("style",styleOverlayValue);
		$("#form_overlay").fadeTo("fast", opac);
        
        $("#"+showID).addClass("lb_active");
        $("#"+showID).attr("style",styleLightboxValue);
        $("#"+showID).fadeIn(1200);
        
        $("#"+showID+" .lb_close").click(popupLbClose);
}

function popupLbClose()
{
	//for ie6 bug
	$("select").attr("style","visibility:show;");
	$(".lb_active").fadeOut("normal");
	$("#form_overlay").fadeOut("normal");
    $(".lb_active").removeClass("lb_active");
}

function init_ajax_calculate()
{
    $("#calc_form").submit(function() {
        $(this).ajaxSubmit({ 
            success: function(resp) {
                $("#calc_result").html(resp);
                $("#calc_result").slideDown("normal");
            }
        });
    });
  
    /*ajax
    $.ajax({
        data: param,
        url: curUrl,
        type: "GET",
        cache: false,
        success: function(html){
            $("#table_calculated").html(html);
    $("#calculateBox").slideDown("normal");
        }
    });
    */
}

function contSearchShowValue(sValId)
{
    var arrValId = sValId.split("###");
    
    htmlValId = $("#"+arrValId[2]).html();
    
    if(arrValId[2] == 'input_value') tmpVal = $("#field_search_value .search").val();
    $("#field_search_value").html(htmlValId);
    if(arrValId[2] == 'input_value') tmpVal = $("#field_search_value .search").val(tmpVal);
}

function syncInputVal(syncVal) {
    $("#input_value .search").val(syncVal);
    //alert($("#input_value .search").val());
}
