/* jquery powered javascript for mikaelgeljic.com
 * by/for Mikaël Geljic
 * |\/|.GELJIC [à] G|\/|AIL
 */
var currentIndex = 0;
var showIndex = 1;
var ww;
$(document).ready(function(){
window.scrollTo(0, 1);
disableControls();
if(document.location.hash) {
switch(document.location.hash) {
case '#about' : currentIndex=0; break;
case '#networks' : currentIndex=1; break;
case '#contact' : currentIndex=2; break;
case '#links' : currentIndex=3; break;
default : currentIndex=0;
}
}
updateIndex();
updateActiveLink();
$('a#vcf').click(function() {window.open("contents/mikaelgeljic.vcf");});
$('a.cven').click(function() {window.open("contents/cv-geljic-mikael-2010-nov-en.pdf");});
$('a#cvfr').click(function() {window.open("contents/cv-geljic-mikael-2010-nov-fr.pdf");});
$('a.extranav').bind('click', slideToItem);
$('textarea#mg-contact-msg').focus(function(){
$(this).val('').unbind('focus');
});
$('input#mg-contact-email').focus(function(){
$(this).val('').unbind('focus');
});
$('input#thebadtouch').val('ajax');
$('form#mg-contact-form').attr('action', '#contact');
$('input#mg-contact-send').click(function(){
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
$("#mg-contact-email").trigger('focus');
var emailFromVal = $("#mg-contact-email").val();
if(emailFromVal == '' || emailFromVal == 'Your e-mail') {
$("#mg-contact-email").val('e-mail should not be empty.').css({'color':'#c00','border-color':'#c00'}).unbind('focus').focus(function() {
$(this).val(emailFromVal).css({'color':'#444','border-color':'#999'}).unbind('focus');
});
hasError = true;
} else if(!emailReg.test(emailFromVal)) {
$("#mg-contact-email").val('e-mail should be valid.').css({'color':'#c00','border-color':'#c00'}).unbind('focus').focus(function() {
$(this).val(emailFromVal).css({'color':'#444','border-color':'#999'}).unbind('focus');
});
hasError = true;
}
$("#mg-contact-msg").trigger('focus');
var messageVal = $("#mg-contact-msg").val();
if(messageVal == '' || messageVal == 'Your message...') {
$("#mg-contact-msg").val('Your message should not be empty.').css({'color':'#c00','border-color':'#c00'}).unbind('focus').focus(function() {
$(this).val(messageVal).css({'color':'#444','border-color':'#ccc'}).unbind('focus');
});
hasError = true;
}
$(this).trigger('focus');
if(hasError == false) {
$(this).unbind('click').click(function() {return false;});
$('#mg-contact-msg').attr('disabled','disabled');
$('#mg-contact-email').attr('disabled','disabled');
$(this).attr('disabled','disabled').val('Wait');
$.post("sendit.php", {'mgemail':emailFromVal, 'mgmsg':messageVal, 'thebadtouch':'ajax'}, function(data) {
$("input#mg-contact-send").val('Sent').removeAttr('disabled').css({'color':'#fff','background-color':'#393','cursor':'none'});
});
}
return false;
});
$(document).keypress(function(e) {
if(e.keyCode == 37 || e.charCode == 37) {
$('a#larrow').trigger('click');
return false;
}
if(e.keyCode == 39 || e.charCode == 39) {
$('a#rarrow').trigger('click');
return false;
}
});
$("div#showcase div.sect div.item").hide();
$("div#showcase div.sect div.show1").fadeIn();
$("span.quo").bind('click', showcase);

$(window).bind('resize', function() {
ww = $('div.wrapper').width();
}).trigger('resize');
});
function slideToItem() {
disableControls();
var num = $(this).attr('id').substr(1,1);
currentIndex = num - 1;
updateActiveLink();
$('div.wrapper').animate({scrollLeft: ww*currentIndex}, 600, updateIndex);
return false;
}
function slideLeft() {
disableControls();
currentIndex--;
updateActiveLink();
$('div.wrapper').animate({scrollLeft: ww*currentIndex}, 600, updateIndex);
return false;
}
function slideRight() {
disableControls();
currentIndex++;
updateActiveLink();
$('div.wrapper').animate({scrollLeft: ww*currentIndex}, 600, updateIndex);
return false;
}
function dummy() {
return false;
}
function disableControls() {
$('a#larrow').unbind('click').bind('click', dummy);
$('a#rarrow').unbind('click').bind('click', dummy);
$('ul.menu li a').unbind('click').bind('click', dummy).removeClass('active');
$('ul.menu li a').removeClass('active');
}
function updateIndex() {
$('ul.menu li a').unbind('click').bind('click', slideToItem);
if(currentIndex>0) $('a#larrow').unbind('click').bind('click', slideLeft);
if(currentIndex<3) $('a#rarrow').unbind('click').bind('click', slideRight);
updateHash();
}
function updateActiveLink() {
switch(currentIndex) {
case 0 : $('a#a1').addClass('active'); break;
case 1 : $('a#a2').addClass('active'); break;
case 2 : $('a#a3').addClass('active'); break;
case 3 : $('a#a4').addClass('active'); break;
}
}
function updateHash() {
switch(currentIndex) {
case 0 : document.location.hash = "about"; break;
case 1 : document.location.hash = "networks"; break;
case 2 : document.location.hash = "contact"; break;
case 3 : document.location.hash = "links"; break;
}	
}
function showcase() {
$("span.quo").unbind('click');
var s = $(this).html();
if(s == "»" && showIndex <3) {
$("div#showcase div.sect div.show"+showIndex).fadeOut('fast', function() {
showIndex++;
$("div#showcase div.sect div.show"+showIndex).fadeIn();
$("span.quo").bind('click', showcase);
});
} else if(s=="«" && showIndex >1) {
$("div#showcase div.sect div.show"+showIndex).fadeOut('fast', function() {
showIndex--;
$("div#showcase div.sect div.show"+showIndex).fadeIn();
$("span.quo").bind('click', showcase);
});
}
}
