var menu_last;
var last_work_sec;
var last_work_num;
var lock_show_work;
var fx_page;
var fx_fp;
var showed_work_first_time;

last_work_sec = "";
last_work_num = "";
lock_show_work = 0;
showed_work_first_time = 0;

function ShowFolio(){
    //parent['frame2'].height = "100%";
    //parent['frame1'].height = "0px";
    //alert('YIKES');
}

function BodyLoad2(){
    instant_fx_page = new fx.Height('page', {duration: 0});
    instant_fx_fp = new fx.Height('fp', {duration: 0});
    
    fx_page = new fx.Height('page', {duration: 300});
    fx_fp = new fx.Height('fp', {duration: 300});
    
    instant_fx_page.hide();
    
    setTimeout('ShowFolio()', 500);
}

function ShowNow(what){
    fx_fp.toggle();
    fx_page.toggle();
    
    setTimeout("fx_fp.hide()", 350);
    //$("fp").style.display = 'none';
    //$("fp").style.visibility = 'hidden';
    //$('fp').style.position = 'absolute';
    //$('fp').style.top = '0';
    
    //setTimeout("$('folio').removeChild($('fp'))", 450);
    
    BodyLoad(what);
}

function BodyLoad(showitnow){

    instant_fx_works = new fx.Height('content_works', {duration: 0});
    instant_fx_profile = new fx.Height('content_profile', {duration: 0});
    instant_fx_contacts = new fx.Height('content_contacts', {duration: 0});
    
    fx_works = new fx.Height('content_works', {duration: 250});
    fx_profile = new fx.Height('content_profile', {duration: 250});
    fx_contacts = new fx.Height('content_contacts', {duration: 250});
    
    Element.cleanWhitespace('content');
    init();
    MakeWorkList();
    
    switch(showitnow){
	case "works":
	    instant_fx_profile.hide();
	    instant_fx_contacts.hide();
	    menu_last = showitnow;
	    $('menu_works').className = "menu_select";
	    ShowWork('identity', 1);
	    showed_work_first_time = 1;
	    break;
	case "profile":
	    instant_fx_works.hide();
	    instant_fx_contacts.hide();
	    menu_last = showitnow;
	    $('menu_profile').className = "menu_select";
	    break;
	case "contacts":
	    instant_fx_profile.hide();
	    instant_fx_works.hide();
	    menu_last = showitnow;
	    $('menu_contacts').className = "menu_select";
	    break;
    }
   
    initLightbox();
    
}

function init(){
		
		var stretchers = document.getElementsByClassName('stretcher'); //div that stretches
		var toggles = document.getElementsByClassName('display'); //h3s where I click on

		//accordion effect
		var myAccordion = new fx.Accordion(
			toggles, stretchers, {opacity: true, duration: 500}
		);

		//hash functions
		var found = false;
		toggles.each(function(h3, i){
			var div = Element.find(h3, 'nextSibling'); //element.find is located in prototype.lite
			if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(div);
				found = true;
			}
		});
		if (!found) myAccordion.showThisHideOpen(stretchers[0]);
	}

function ShowMe(what){
    switch(what){
        case 'works':   (menu_last=='works')?void(0):fx_works.toggle();
                        (menu_last=='profile')?fx_profile.toggle():fx_profile.hide();
                        (menu_last=='contacts')?fx_contacts.toggle():fx_contacts.hide();

			if(showed_work_first_time == 0){
			    showed_work_first_time = 1;
			    ShowWork('identity', 1);
			}

                        $('menu_works').className = "menu_select";
                        $('menu_profile').className = "menu_normal";
                        $('menu_contacts').className = "menu_normal";
                        
                        menu_last = what;
                        
                        break;
        
        case 'profile': (menu_last=='works')?fx_works.toggle():fx_works.hide();
                        (menu_last=='profile')?void(0):fx_profile.toggle();
                        (menu_last=='contacts')?fx_contacts.toggle():fx_contacts.hide();
                        
                        $('menu_works').className = "menu_normal";
                        $('menu_profile').className = "menu_select";
                        $('menu_contacts').className = "menu_normal";
                        
                        menu_last = what;
                        
                        break;
    
        case 'contacts':(menu_last=='works')?fx_works.toggle():fx_works.hide();
                        (menu_last=='profile')?fx_profile.toggle():fx_profile.hide();
                        (menu_last=='contacts')?void(0):fx_contacts.toggle();
                        
                        $('menu_works').className = "menu_normal";
                        $('menu_profile').className = "menu_normal";
                        $('menu_contacts').className = "menu_select";
                        
                        menu_last = what;
                        
                        break;
    }

}

function ShowWork(sec, num){
    if(last_work_sec == sec && last_work_num == num) return;
    
    if(lock_show_work==1) return;
    lock_show_work = 1;
    
    $(sec + '_' + num).className = "menu_select";
    (last_work_sec!="" && last_work_num!="")?$(last_work_sec + '_' + last_work_num).className = "menu_normal":void(0);

    last_work_sec = sec;
    last_work_num = num;
    
    var image1;
    image1 = "";
    image1 = new Image();
    image1.src = "img/work/preview/" + portfolio['en'][sec][num]['preview'];
    
    document.getElementById('sw').innerHTML = document.getElementById('preview').innerHTML;
    /*
    $('preview').innerHTML = '<a href="img/work/' + portfolio['en'][sec][num]['work'][1] + '" rel="lightbox[roadtrip]" title="my caption"><img src="img/work/preview/' + portfolio['en'][sec][num]['preview'] + '" width="240px" height="125px" /></a><br>';
    */
    
    $('preview').innerHTML = '<a href="img/work/' + portfolio['en'][sec][num]['work'][1] + '" rel="lightbox" desc="<h5>' + portfolio['en'][sec][num]['name'] + '</h5><h4>' + portfolio['en'][sec][num]['description'] + '</h4><br><br>"><img id="' +sec + num + '" src="img/work/preview/' + portfolio['en'][sec][num]['preview'] + '" width="240px" height="125px" /></a><br>';
    /*
    $('preview').innerHTML = '<img src="http://mstarskate.com/img/work/preview/' + portfolio['en'][sec][num]['preview'] + '" width="240px" height="125px" /><br>';
    
    $('preview').innerHTML = '<a href="img/work/' + portfolio['en'][sec][num]['work'][1] + '" rel="lightbox" desc="<h5>' + portfolio['en'][sec][num]['name'] + '</h5><h4>' + portfolio['en'][sec][num]['description'] + '</h4><br><br>"><img src="trans.gif" style="background-url:(img/work/preview/' + portfolio['en'][sec][num]['preview'] + ');" width="240px" height="125px" /></a><br>';
    */
    //$('sw').innerHTML = image1.src;
    image1.src = "img/work/preview/" + portfolio['en'][sec][num]['preview'];
    
    $(sec + num).style.backgroundImage = 'url(img/work/preview/' + portfolio['en'][sec][num]['preview'] + ')';
    
    (portfolio['en'][sec][num]['name']!='')? $('preview').innerHTML += '<br/><h4>NAME : </h4> ' + portfolio['en'][sec][num]['name'] : void(0);
    (portfolio['en'][sec][num]['date']!='')? $('preview').innerHTML += '<br/><h4>DIMENSIONS : </h4> ' + portfolio['en'][sec][num]['date'] : void(0);
    (portfolio['en'][sec][num]['client']!='')? $('preview').innerHTML += '<br/><h4>MANUFACTURER : </h4> ' + portfolio['en'][sec][num]['client'] : void(0);
    (portfolio['en'][sec][num]['url']!='')? $('preview').innerHTML += '<br/><h4>URL : </h4> ' + portfolio['en'][sec][num]['url'] : void(0);
    (portfolio['en'][sec][num]['description']!='')? $('preview').innerHTML += '<br><br><div id="works_description"><h4>Description : </h4> ' + portfolio['en'][sec][num]['description'] + '</div>' : void(0);
    
    
    instantHide = new fx.Height('sw', {duration: 0});
    hideSwitch = new fx.Height('sw', {duration: 200}); //smaller
                
    myResize = new fx.Height('preview', {duration: 400}); //bigger
    myResize.hide();
    
    setTimeout("myResize.toggle();", 20);
    instantHide.hide(); instantHide.toggle();
    setTimeout("hideSwitch.toggle();", 15);

    initLightbox();
    setTimeout("UnLock();", 750);
}

function UnLock(){
    lock_show_work = 0;
}

function SubmitComplete(request){
    //alert (request.responseText);
    $('contact_name').value = "";
    $('contact_mail').value = "";
    $('contact_message').value = "";
    $('contact_thanks').innerHTML = "<br/><br/>";
    $('thnx').innerHTML = "Thank you for your Message.";
}

function SubmitForm(){
    var retval;
    var mssg;
    retval = 0;
    mssg = "";
    
    $('thnx').innerHTML = "";
    
    if($('contact_name').value == ""){
	mssg += "&nbsp;&nbsp;" + "Enter your name, please." + "&nbsp;&nbsp;<br/>";
	retval += 2;
    }
    
    if($('contact_message').value == ""){
	mssg += "&nbsp;&nbsp;" + "Leave me a few words in message." + "&nbsp;&nbsp;";
	retval += 4;
    }
    
    if(retval>0){
	$('contact_thanks').innerHTML = mssg;
    }
    
    if(retval==0){
	body_message  = "name=";
	body_message += $('contact_name').value;
	body_message += '&mail=';
	body_message += $('contact_mail').value;
	body_message += '&message=';
	body_message += $('contact_message').value;
	
	new ajax('form2.php', {postBody: body_message, update: $('thnx'),onComplete: SubmitComplete});
    }
    
}

function MakeWorkList(){
    for(var section in portfolio['en']){
        for (var number in portfolio['en'][section]){
            $('works_'+section).innerHTML += '<a class="menu_normal" href="javascript:void(0)" id="' + section + '_' + number + '" onclick="ShowWork(\'' + section + '\',\'' + number + '\')">' + number + '</a>'
        }
    }
}