function getPosition(e){
    var x = 0, y = 0, width = e.offsetWidth || 0, height = e.offsetHeight || 0;
    do{
        x += e.offsetLeft;
        y += e.offsetTop;           
    }while((e = e.offsetParent) && e.tagName && e.tagName.toLowerCase() != 'body');
           
    return {
        x:x,
        y:y,
        w:width,
        h:height
    };
}

// Custom Functions
var touts = new Array();
function dropDown(menu){
    var div = 'menu_'+menu;
    document.getElementById(div).style.visibility='visible';
    clearTimeout(touts[menu]);
}
function dropUp(menu){
    var delay = 2000;
    var div = 'menu_'+menu;
    touts[menu] = setTimeout("document.getElementById('"+div+"').style.visibility='hidden'",delay);
}

function posAll(){
    posMenus();
}

function posMenus(){
    //mainmenu shrink to fit
    /*var width = 0;
	var eleDiv = document.getElementById('menu');
	var eleNodes = eleDiv.childNodes;
	for(i=0;i<eleNodes.length;i++){
		var eleNode = eleNodes[i];
		var tpx = getPosition(eleNode);
		width += tpx.w;	
	}
	eleDiv.style.width = width+'px';*/
	
    //Submenus (pos the holding div)
    for(i=0;i<menu.length;i++){
        var div = menu[i].toString();
        var subDiv = 'menu_'+div;
        var eleSubDiv = document.getElementById(subDiv);
        var eleDiv = document.getElementById(div);
        var offset = 5;
        var tpx = getPosition(eleDiv);
        eleSubDiv.style.left = tpx.x+'px';
        eleSubDiv.style.top = tpx.y+tpx.h+offset+'px';
        i+=1;
    }
}

function previewSwap(page,index){
    var tmpPreviewObj = window["previewImagesObj"+page];
    var tmpPreviewImage = 'image'+page;
    document.getElementById(tmpPreviewImage).src = tmpPreviewObj[index].src;
//alert('PSCall');
}

/*function fadeImage(name/*,fadeDelay,fadeAmount){
    fadeDelay = 1;
    fadeAmount = 0.05;
    //try{
    posName = window["pos"+name];
    //ssName = 'slideshow'+name;
    ssName = 'slide'+posName;
    //alert(name);
    //ucfName = name.substr(0, 1).toUpperCase() + name.substr(1);
    $('#'+ssName).fadeOut("slow",updateImage(name,6000));
/*opacity = document.getElementById(ssName).style.opacity-fadeAmount;
    document.getElementById(ssName).style.opacity = opacity;
    document.getElementById(ssName).style.filter = 'alpha(opacity:'+(opacity*100)+')';
    if(document.getElementById(ssName).style.opacity>0)
        window.setTimeout('fadeImage(\''+name+'\');',fadeDelay);
    else{
        document.getElementById(ssName).style.visibility = 'hidden';
        window.setTimeout('updateImage(\''+name+'\',\'6000\');',2000);
    }
//}catch(err){}
}

function updateImage(name,showDelay){
    posName = window["pos"+name];
    numName = window["numImages"+name];
    imgName = window["imagesObj"+name];
    imgID = 'ssIMG'+name;
    titlesName = window["titles"+name];
    //ssName = 'slideshow'+name;
    ssName = 'slide'+posName;

    if(posName == numName){
        window["pos"+name] = 0;
        posName = window["pos"+name];
    }
    //content = '<div id=\"ssImage\"><img id=\"'+imgID+'\" src=\"'+imgName[posName].src+'\"></div><div id=\"ssTitles\">'+titlesName[posName]+'</div>';
    //document.getElementById(ssName).innerHTML = content;
    window["pos"+name] = posName+1;
    document.getElementById(ssName).style.visibility = 'visible';
    document.getElementById(ssName).style.opacity = 0.95;
    document.getElementById(ssName).style.filter = 'alpha(opacity:100)';
    window["ssTimeout"] = window.setTimeout('fadeImage(\''+name+'\');',showDelay);
}
$.fn.swapImage = function(name){
    var posName = window["pos"+name];
    var ssName = 'slide'+posName;
    var numName = window["numImages"+name];
       
    if(posName == numName)
        window["pos"+name] = 0;
    else
        window["pos"+name] = posName+1;
    
    posName = window["pos"+name];
    ssName = 'slide'+posName;
    //$('#'+ssName).fadeIn("slow",swapImage(name));
    //startSlideshow(name);
    return $('#'+ssName);
}*/
function startSlideshow(name){
    var ssName = 'slideshow'+name;
    var ssFX = window["ssFX"+name];
    //alert(ssFX);
    $('#'+ssName).cycle({
	fx: ssFX,
        timeout:  8000,
        pause: 1
    });
}

function sendForm(form){
     /*$.post("/global/scripts/index.php?special="+form, $("."+form).serialize(),function(result){
         //and after the ajax request ends we check the text returned
         if(result == 'sent'){
             //if the mail is sent remove the submit paragraph
              //$('#cf_submit_p').remove();
             //and show the mail success div with fadeIn
             //$('#mail_success').fadeIn(500);
         }else{
             //show the mail failed div
             //$('#mail_fail').fadeIn(500);
             //reenable the submit button by removing attribute disabled and change the text back to Send The Message
             //$('#send_message').removeAttr('disabled').attr('value', 'Send The Message');
         }
    });*/
    
    $.ajax({
        type: "GET",
        url: "/global/scripts/index.php",
        data: $("#"+form).serialize()+"&special="+form,
        dataType: 'html',
        timeout: 1000,
        async:false,
        cache: false,
        success: function(html){
            $(".main").html(html);
        }
    });
}

function loadPage(page,dir,file){
    clearTimeout(window["ssTimeout"]);
    //$(".cell_right").css({'background' : 'url(spinner.gif) no-repeat center center'});

    $.ajax({
        type: "GET",
        url: "/global/scripts/index.php",
        data: "page="+escape(page)+"&dir="+dir+"&file="+escape(file),
        dataType: 'html',
        timeout: 1000,
        async:false,
        cache: false,
        success: function(html){ //so, if data is retrieved, store it in html
            $(".main").html(html);
        }
    });

    //check for SSHow
    addon = window["startSlide"+page];
    if(addon){
        var max = window["numImages"+page];
        var name = 'slideshow'+page;
        imgName = window["images"+page];
        titlesName = window["titles"+page];
        imagesObj = window["imagesObj"+page];
        
        for(i=0;i<max;i++){
            imagesObj[i] = new Image();
            // load image
            $(imagesObj[i]).load(function () {
                }).error(function () {
                $('ssImage').remove();
            }).attr('src', imgName[i]);

            //append layer
            $("#"+name).append('<div id="slide'+i+'" style="position:absolute;"><div id=\"ssImage\"><img src="'+imagesObj[i].src+'"></div><div id="ssTitles">'+titlesName[i]+'</div></div>');
        }
        startSlideshow(page);
    }

    //check for Preview
    addon = window["startPreview"+page];
    if(addon){
        max = 4;
        for(i=1;i<max+1;i++){
            imgName = window["previewImages"+page];
            imagesObj = window["previewImagesObj"+page];
            imagesObj[i] = new Image();
            // load image
            $(imagesObj[i]).load(function () {
                }).error(function () {
                }).attr('src', imgName[i]);
        }
        previewSwap(page,1);
    }
}

function formGrow(){
    $("#cell_left").animate({
        width: "470px"
      }, 2000 );
      $("#cell_right").css({ 'width' : '443px' });
      $("label").animate({ fontSize: "12px"},2000);
      $("input").animate({ fontSize: "12px"},2000);
      $("#commentForm").css({ 'visibility' : 'visible' });
}

function formShrink(){
    $("#commentForm").css({ 'visibility' : 'hidden', 'width' : '50px'});
    $("#cell_left").animate({
        width: "860px"
      }, 2000, function(){$("#cell_right").css({ 'width' : '50px' });});
      
    //$("#commentForm").validate().resetForm();
      $("label").animate({ fontSize: "10px"},2000);
      $("input").animate({ fontSize: "10px"},2000);
}
function light(){
    $("#light").animate({
        marginLeft: "450px",
        opacity: 1.0
      }, 3000 ).animate({
        marginLeft: "900px",
        opacity: 0.0
      }, 3000 );

      window.setTimeout('light();',5000);
}

$(document).ready(function(){
    loadPage('home','pages','1_home.html');
    light();
    $("#commentForm").validate();
    $(".cell_right").mouseenter(function (){formGrow();}).mouseleave(function (){formShrink();});
});
