/*!
 * Art gallery website
 *
 * Copyright 2010, Dinca Andrei
 * andrei.webdeveloper@gmail.com
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: 07.05.2010
 !*/
$(document).ready(function(){

	// layout initialization
	initLayouts();
	
	// website controler
	var websiteControl = { 
		/* generic/layout settings */ 
		'startPage': '#home',  // change start page
		'caruselWrapper': '#caruselWrapper',
		
		/* navigation settings */ 
		'nav_speed': 850,
		'nav_easing': 'easeInOutExpo',
		'nav_menuSelectedClass': 'selected',
		'nav_callback': "",
		
		/* services settings */ 
		'services_wrapper': '.contentTextSlide',
		'services_speed': 850,
		'services_easing': 'easeInOutExpo',
		
		/* gallery carusel */
		'carusel_wrapper': '.galleryCarusel',
		'carusel_stepSize': 795,
		'carusel_imgItems': '.imgSlideshow',
		'carusel_speed': 350,
		'carusel_easing': 'easeInOutExpo',
		'carusel_callback': ""
	}; 


	$('.nav').click(function () {
		var moveTo = 0;
		var stepSize = websiteControl['carusel_stepSize'];
		var direction = $(this).attr('class').replace("nav ", "");
		var currentPage = $(this).parent().parent();
		var currentPos = currentPage.find(websiteControl['carusel_wrapper']).scrollLeft();
		var limitSize = ((currentPage.find(websiteControl['carusel_imgItems']).size() / 3) * stepSize) - stepSize;
		
		if(direction == 'next'){
			if(currentPos < limitSize){
				moveTo = currentPos + stepSize;
			}else{
				moveTo = 0;
			}
		} else if(direction == 'prev'){
			if(currentPos > 0){
				moveTo = currentPos - stepSize;
			}else{
				moveTo = 0;
			}
		}
		
		currentPage.find(websiteControl['carusel_wrapper']).stop().animate({
			scrollLeft : moveTo
		},  websiteControl['carusel_speed'], websiteControl['carusel_easing'], function(){
			// callback function
			eval(websiteControl['nav_callback']);
		});
		
	});
	
	// gallery page lightbox
	$("a.galleryLightbox").fancybox({
		'titleShow'     : true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	// menu services
	$('.menuservices ul li a').click(function(){
		$(".menuservices").find('.selected').removeClass('selected');
		$(this).addClass('selected');
		servicesSlide($(this).attr('rel'), websiteControl);
		return false;
	});
	
	// observer hash change. Work back button.
	$(window).bind('hashchange', function () {
		var hash = window.location.hash || websiteControl['startPage'];
		navigateToPage(hash, websiteControl);
	});
	$(window).trigger("hashchange");
	initLayouts();
	 
}); 

$(window).resize(function() {
	// if window resize
	initLayouts();
});















/*!
 * Art gallery website
 *
 * Copyright 2010, Dinca Andrei
 * andrei.webdeveloper@gmail.com
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: 07.05.2010
 !*/

/*!
 * navigate to specific page slider
 * on event: hashchange
!*/
var navigateToPage = function(currentPage, websiteControl){ 
	if(currentPage == ""){ // if no hash send
		return false;
	} 
	// make menu selected
	var temMenu = str_replace("#", 'menu_', currentPage); 
	$(".menu ul li").find("." + websiteControl['nav_menuSelectedClass']).removeClass('selected');
	$("." + temMenu).addClass(websiteControl['nav_menuSelectedClass']);
	
	// process request
	currentPage = str_replace("#", 'section', currentPage);
	var position = $("li#" + currentPage).position();
	var toLeft = parseInt(position.left); 
	var tempWidth = (($("body").width() - $("li#" + currentPage).width()) / 2);
	var tempMarginLeft = parseInt($("li#" + currentPage).css('margin-left'));
	toLeft = toLeft + ((tempMarginLeft - tempWidth));
	
	$(websiteControl['caruselWrapper']).animate({
		scrollLeft : toLeft
	},  websiteControl['nav_speed'], websiteControl['nav_easing'], function(){
		// callback function
		eval(websiteControl['nav_callback']);
	});
	return false;
}

/*!
 * Services page slider
 * on event: On click
!*/
var servicesSlide = function(moveTo, websiteControl){
	$(websiteControl['services_wrapper']).stop().animate({
		scrollTop : moveTo
	},  websiteControl['services_speed'], websiteControl['services_easing'], function(){	
	});
}

/*!
 * Init layout
 * on event: onload, window resize
!*/
var initLayouts = function(){
	// adjust width fake
	if($("body").width() < 900 ) $("body").width(900);
	var tempWidth = (($("body").width() - 850) / 2) - 50;
	$("#sectionfake").width(tempWidth);
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
