var option2;
var c=0;
function setOptions(option) {

    // swfaddress
    if (SWFAddress.getPath()!="\\") {
        var path=SWFAddress.getPath();
        if (path) {
            var pathArr=path.split("/");
            option=pathArr[1];
            option2=pathArr[2];
            if (option=="gallery") loadGallery(option2);
        }
    }

    var about = new Fx.Slide('about');
    var projects = new Fx.Slide('projects');
    var gallery = new Fx.Slide('gallery');
    var contact=new Fx.Slide('contact');
    var showContent = new Fx.Slide('content');
    var optionAnimation=false;
    showContent.addEvent('onComplete',function() {optionAnimation=false;});    

    var showAbout=false;
    var showProjects=false;
    var showGallery=false;
    var showContact=false;    

    contact.hide();
    about.hide();
    projects.hide();
    gallery.hide();
    $('close').hide();   

    if (option=="gallery") {
        showGallery=true;
        gallery.show();
        $('close').show();
        showContent.hide();
    }
    else if (option=="contact") {
        showContact=true;
        contact.show();
        $('close').show();
        showContent.hide();
    }
    else if (option=="projects") {
        showProjects=true;
        projects.show();
        $('close').show();
        showContent.hide();
    }
    else if (option=="about") {
        showAbout=true;
        about.show();
        $('close').show();
        showContent.hide();
    }
    
    $('slideAbout').addEvent('click', function(e){
        if (!optionAnimation){
            e = new Event(e);
            projects.slideOut();
            gallery.slideOut();
            contact.slideOut();
            showProjects=false;
            showContact=false;
            showGallery=false;
            optionAnimation=true;
            if (!showAbout) {
                about.slideIn();
                showAbout=true;
                showContent.slideOut();
                $('close').show();
                SWFAddress.setValue("about");
            }
            else {
                about.slideOut();
                showAbout=false;
                showContent.slideIn();
                $('close').hide();
                SWFAddress.setValue("");
            }
            e.stop();
        }
    });
    $('slideProjects').addEvent('click', function(e){
        if (!optionAnimation){
            e = new Event(e);
            about.slideOut();
            gallery.slideOut();
            contact.slideOut();
            showAbout=false;
            showContact=false;
            showGallery=false;
            optionAnimation=true;
            if (!showProjects) {
                projects.slideIn();
                showProjects=true;
                showContent.slideOut();
                $('close').show();
                SWFAddress.setValue("projects");
            }
            else {
                projects.slideOut();
                showProjects=false;
                showContent.slideIn();
                $('close').hide();
                SWFAddress.setValue("");
            }
            e.stop();
        }
    });
    $('slideGallery').addEvent('click', function(e){
        if (!optionAnimation){
            e = new Event(e);
            about.slideOut();
            projects.slideOut();
            contact.slideOut();
            showAbout=false;
            showContact=false;
            showProjects=false;
            optionAnimation=true;
            if (!showGallery) {
                gallery.slideIn();
                showGallery=true;
                showContent.slideOut();
                $('close').show();
                SWFAddress.setValue("gallery");
                loadGallery();
            }
            else {
                gallery.slideOut();
                showGallery=false;
                showContent.slideIn();
                $('close').hide();
                SWFAddress.setValue("");
            }
            e.stop();
        }
    });
    $('slideContact').addEvent('click', function(e){
        if (!optionAnimation){
            e = new Event(e);
            about.slideOut();
            projects.slideOut();
            gallery.slideOut();
            showAbout=false;
            showGallery=false;
            showProjects=false;
            optionAnimation=true;
            if (!showContact) {
                contact.slideIn();
                showContact=true;
                showContent.slideOut();
                $('close').show();
                SWFAddress.setValue("contact");
            }
            else {
                contact.slideOut();
                showContact=false;
                showContent.slideIn();
                $('close').hide();
                SWFAddress.setValue("");
            }
            e.stop();
        }
    });
    $('closeOptions').addEvent('click', function(e){
        e = new Event(e);
        about.slideOut();
        projects.slideOut();
        gallery.slideOut();
        contact.slideOut();
        showAbout=false;
        showGallery=false;
        showProjects=false;
        showContact=false;
        showContent.slideIn();
        $('close').hide();
        e.stop();
        SWFAddress.setValue("");
    });
    // code highlight
    SyntaxHighlighter.config.stripBrs = true;
    SyntaxHighlighter.all();
}
function checkResolution() {
//    if (($('content').getSize().y>$('main').getSize().y)) $('pageContent').setStyle("min-height",$('content').getSize().y);
//    else $('pageContent').setStyle("min-height",$('main').getSize().y - 180);
	$('pageContent').setStyle("min-height",document.body.offsetHeight - 180);
}
function setPosts() {
    var posts = new Array();
    posts.push($('contentPost0'));
    var s=new Fx.Slide($('contentPost0'));
    s.hide();    
    $('post0').addEvent('click', function(e){
            e = new Event(e);
            s = new Fx.Slide(posts[0]);
            s.toggle();
            e.stop();
    });
    var c = $('postsContentAjax').getChildren('h2');
    $each(c,function(e) {
        posts.push('content' + e.id);
        var s=new Fx.Slide($('content' + e.id));
        s.hide();
        $(e.id).addEvent('click', function(e){
            e = new Event(e);
            s=new Fx.Slide(posts[posts.indexOf('content' + e.target.id)]);            
            s.toggle();
            e.stop();
        });
    });    
    $('twitter').src="http://twitter.com/statuses/user_timeline/peixinhopt.json?callback=twitterCallback2&count=5";
    
}

function deselectText(field,defaultValue) {
    if (field.value.length==0) {
        field.value=defaultValue;
    }
}
function FocusText(field,defaultValue) {
    if (field.value==defaultValue) {
        field.value="";
    }
}
function loadPosts(page,search) {

    if (!page) page=1;
    if (!search || $('search').value=="Search Website...") search="";
    else search=$('search').value;
    
    $('postsContentAjax').innerHTML="<p style=\"text-align:center;margin-top:100px;\"><img src=\"/src/images/loader.gif\" alt=\"loading\" title=\"loading\" width=\"16\" height=\"11\" style=\"border: 0px;\" /> Loading Posts... </p>";
    new Request.HTML({
        url: '/src/ajax/postsList.php?page=' + page + '&search=' + search + '&' + cleanCache(),
        method: 'get',
        update: 'postsContentAjax',
        evalScripts: true, /* this is the default */
        onComplete: function(){setPosts();}
    }).send();
}
function changeColor(fields,color1,color2) {
    fields.each(function(field) {
        $(field).morph({'backgroundColor':color1}).morph({'backgroundColor':color2});
    });
//    $('nameContact').morph('.contacts').morph('.formERROR');
//    $('textContact').morph('.contacts').morph('.formERROR');
}
function throwErrorWindow(field) {
	$(field).setStyle('display', "block");
	$(field).setStyle('opacity',0);
	$(field).set('tween', {
		duration:200,
		onComplete: function(e) {
				$(e.id).set('tween', { duration:1000,onComplete: function(e) {
					$(e.id).set('tween', { onComplete: Class.empty });
					$(e.id).tween('opacity', 0);
				}});
				$(e.id).tween('opacity',1);
			}
		});
	$(field).tween('opacity', 1);

}
function showSendingWindow(field) {
    $(field).setStyle('display', "block");
	$(field).setStyle('opacity',0);
	$(field).set('tween', { duration:200 });
	$(field).tween('opacity', 1);
}
function hideSendingWindow(field) {
	$(field).setStyle('opacity',1);
	$(field).set('tween', { duration:2000 });
	$(field).tween('opacity', 1).tween('opacity',0);
}
function sendContact() {
    $('sendingContact').setStyle('backgroundColor', '#35211F');
    var error=false;
    if ($('nameContact').value.length==0 || $('nameContact').value=="Name") {
    	changeColor(['nameContact'], '#C4C4C4','#993333');
    	error=true;
    }
    else changeColor(['nameContact'],'#993333','#C4C4C4');
    if ($('textContact').value.length==0) {
    	changeColor(['textContact'], '#C4C4C4','#993333');
        error=true;    	
    }
    else changeColor(['textContact'],'#993333','#C4C4C4');
    if ($('mailContact').value.length==0 || $('mailContact').value=="Mail") {
    	changeColor(['mailContact'], '#C4C4C4','#993333');
        error=true;
    }
    else changeColor(['mailContact'],'#993333','#C4C4C4');
    if (error) {
        throwErrorWindow('contactError');
    }
    else {
        $('sendingContact').innerHTML="<h2 class='sendingH2'>Sending Contact!</h2><br /><img src='/src/images/loader.gif' alt='loading' title='loading' width='16' height='11' style=\"border: 0px;\" /> Please Wait...";
        showSendingWindow('sendingContact');
        new Request.HTML({
        url: '/src/ajax/saveCommentContact.php?type=contact&nameContact=' + $('nameContact').value + '&mailContact=' + $('mailContact').value + '&homepageContact=' + $('homepageContact').value + '&textContact=' + $('textContact').value.replace("\n", "\n"),
        method: 'get',
        update: 'sendingContact',
        evalScripts: true, /* this is the default */
        onComplete:function() {
            hideSendingWindow('sendingContact');
        }
    }).send();
    }
}
function sendComment(idNew) {
    $('sendingComment').setStyle('backgroundColor', '#35211F');
    var error=false;
    if ($('nameComment').value.length==0 || $('nameComment').value=="Name") {
    	changeColor(['nameComment'], '#FFFFFF','#993333');
    	error=true;
    }
    else changeColor(['nameComment'],'#993333','#FFFFFF');
    if ($('textComment').value.length==0) {
    	changeColor(['textComment'], '#FFFFFF','#993333');
    	error=true;        
    }
    else changeColor(['textComment'],'#993333','#FFFFFF');
    if ($('mailComment').value.length==0 || $('mailComment').value=="Mail") {
    	changeColor(['mailComment'], '#C4C4C4','#993333');
        error=true;
    }
    else changeColor(['mailComment'],'#993333','#FFFFFF');
    if (error) {
        throwErrorWindow('commentError');        
    }
    else {
        $('sendingComment').innerHTML="<h2 class='sendingH2'>Sending Comment!</h2><br /><img src='src/images/loader.gif' alt='loading' title='loading' width='16' height='11' style=\"border: 0px;\" /> Please Wait...";
        showSendingWindow('sendingComment');

        new Request.HTML({
        url: '/src/ajax/saveCommentContact.php?type=comment&idNew=' + idNew + '&nameComment=' + $('nameComment').value + '&mailComment=' + $('mailComment').value + '&homepageComment=' + $('homepageComment').value + '&textComment=' + $('textComment').value.replace(/\n/g, "<br />"),
        method: 'get',
        update: 'sendingComment',
        evalScripts: true, /* this is the default */
        onComplete:function() {            
            hideSendingWindow('sendingComment');
            getComments(idNew);
        }
    }).send();
    }
}

function getComments(idNew) {
	if (c==0) {
		var result = $('commentsList').getElements('div');
		c = result.length;
	}
    
    var content="";
    // request
    new Request.HTML({
        url: '/src/ajax/getComments.php?idNew=' + idNew + '&' + cleanCache(),
        method: 'get',
        update: 'commentsListNewContent',
        evalScripts: true, /* this is the default */
        onComplete:function() {
            var c1 = $('commentsListNewContent').getElements('div');
            if (c1.length > c) {
            	var i=1;
                var arr=new Array();
                $each(c1, function(e) {
                    if (i>c) {
                    	c++;
                        content+="<div id=\"" + e.id + "\" class=\"" + e.className + "\">" + e.innerHTML + "</div>";
                        arr.push(e.id);
                    }
                    i++;
                });
                $('commentsList').innerHTML+=content;
                $('commentNumber').innerHTML='<a name=\'comments\'>' + c1.length + ' comments</a>';

                $each(arr, function(id) {               
                    new Fx.Slide($(id)).hide().toggle();
                    $(id).tween('opacity', 0,1).start(1000);                    
                });
            }
        }
    }).send();
}

function loadGallery(option) {
        if (option) {
            SWFAddress.setValue("gallery/" + option);
        }
            $('galleryContent').innerHTML="<p style=\"text-align:center;\"><img src=\"/src/images/loader.gif\" alt=\"loading\" title=\"loading\" width=\"16\" height=\"11\" style=\"border: 0px;\" /> Loading Photos... </p>";
            new Request.HTML({
            url: '/src/ajax/getGallery.php?gallery=' + option + '&' + cleanCache(),
            method: 'get',
            update: 'galleryContent',
            evalScripts: true, /* this is the default */
            onComplete: function() {
                Shadowbox.clearCache();
                Shadowbox.init({
                    overlayOpacity: 0.8
                    },Shadowbox.setup("a.galleryPhotosSite", {
                    gallery: "gallery",
                    continuous: true,
                    counterType: "skip"
                }));
            }
            }).send();
        
}

function cleanCache() {
	var d=new Date;
	return "cleanCache=" + d.getTime();
}
