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
function menuClicked(name,dir,file){
    loadPage(name,dir,file);

    //for(i=1;i<window["menu"].length;i++){
            //$('#'+window["menu"][i]).removeClass('menuSel');
            $('.menuItem').removeClass('menuSel');
      //      i++;
    //}
    $('#'+name).addClass('menuSel');
}

var touts = new Array();
function dropDown(menu){
    //Hide all expanded menus to prevent overlapping
    for(i=0;i<window["menu"].length;i++){
            document.getElementById('menu_'+window["menu"][i]).style.visibility='hidden';
            i++;
    }

    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';*/

    //alert(menu.length);
    //Submenus (pos the holding div)
    for(i=0;i<menu.length;i++){
        var div = menu[i].toString();
        var subDiv = 'menu_'+div;
        //alert(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';
        //alert(eleSubDiv.style.top);
        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"]);

    $.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){
                maxImages = window["numImages"+page];
                var name = 'slideshow'+page;
                loadedImages = 0;
                imgName = window["images"+page];
                titlesName = window["titles"+page];
                //imagesObj = new Array(max);
                imagesObj = window["imagesObj"+page];
                //Show loading graphic
                $('#'+name).addClass('loading');
                //alert('loading'+max);
                for(i=0;i<maxImages;i++){
                    imagesObj[i] = new Image();
                    loadImg(imagesObj[i],imgName[i],name,titlesName[i],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 loadImg(obj,url,name,title,page){
    // load image
    $(obj).load(function() {
        //append layer for this Slide
        $("#"+name).append('<div id="slide'+i+'" class="slide"><div id=\"ssImage\"><img src="'+url+'"></div><div id="ssTitles">'+title+'</div></div>');
        window["loadedImages"]++;
        if(window["loadedImages"]==maxImages){
            $('#'+name).removeClass('loading');
            $('.slide').show();
            startSlideshow(page);
        }
    }).error(function () {
        $('ssImage').remove();
    }).attr('src', imgName[i]);
}

function formGrow(){
    if(!formChanging){
        formChanging = true;
    $("#cell_left").animate({
        width: "470px"
      }, 2000, function() {
    // Animation complete.
    formChanging = false;
    if(formClose)
        formShrink();
  });
      $("#cell_right").css({'width' : '443px'});
      $("label").animate({fontSize: "12px"},2000);
      $("input").animate({fontSize: "12px"},2000);
      $("#commentForm").css({'visibility' : 'visible'});
      //$("body").data('formChanging', 'false');
      //alert('boo');
}
}

function formShrink(){
    if(!formChanging){
        formChanging = true;
    //$("#commentForm").css({'visibility' : 'hidden', 'width' : '50px'});
    $("#cell_left").animate({
        width: "860px"
      }, 2000, function(){
          $("#cell_right").css({'width' : '50px'});
          $("#commentForm").css({'visibility' : 'hidden', 'width' : '50px'});
          formChanging = false;
      });
      
    //$("#commentForm").validate().resetForm();
      $("label").animate({fontSize: "10px"},2000);
      $("input").animate({fontSize: "10px"},2000);
      formClose = false;
}else
  formClose = true;
}

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();});
    formChanging = false;
    formClose = false;
    posAll();
});
