$(document).ready(function () {
	// menu 
    var tween = 'easeOutElastic';
    var iniLeft = Math.round($('#menu li.selected').offset().left - $('#menu').offset().left);
    var iniWidth = $('#menu li.selected').width()-6;
 
    $('#box').css({left: iniLeft});
    $('#box .head').css({width: iniWidth});
 
    $('#menu li a').hover(function () {
        left = Math.round($(this).parent().offset().left - $('#menu').offset().left);
        width = $(this).width()-2;

        $('#box').stop(false, true).animate({left: left}, {duration:1000, easing: tween});  
        $('#box .head').stop(false, true).animate({width:width}, {duration:1000, easing: tween});   
    }).click(function () {
        $('#menu li').removeClass('selected'); 
        $(this).parent().addClass('selected');
		switch($(this).attr('id')) {
			case "bthome":
				loadCat(0);
				break;
			case "btmen":
				loadCat(1);
				break;
			case "btwoman":
				loadCat(2);
				break;
			case "btgallery":
				loadCat(4);
				break;
			case "btcontact":
				$('#contact').fadeIn("slow");
				$('#menu').fadeOut("slow");
				break;
			case "btfacebook":
				return true;
				break
		}
		return false;
    });

    $('#menu').mouseleave(function () {
        iniLeft = Math.round($('#menu li.selected').offset().left - $('#menu').offset().left);
        iniWidth = $('#menu li.selected').width()-2;
		
        $('#box').stop(false, true).animate({left: iniLeft},{duration:1500, easing: tween});  
        $('#box .head').stop(false, true).animate({width:iniWidth},{duration:1500, easing: tween});
    });
	
	// blocks footer
	var renderFoots = function() {
		$("div.item").mouseenter(function() {
			$(this).children('.foot').fadeIn("slow");
		}).mouseleave(function() {
			$(this).children('.foot').fadeOut("slow");
		});
	}
	var hide = function(tdiv) {
		$(tdiv).css('visibility', 'hidden');
	}
	var show = function(tdiv) {
		$(tdiv).css('visibility', 'visible');
	}
	
	// parallax
	var parallax = function() {	
		if (!isipad) { 
			if (categ != 1 && categ != 2)  {
				var dizq = $("#contizq .item");
				var tizq = dizq.length;
				var yizq = 498-561*(tizq-1);
				var yini = 416;
			
				$('#contizq').css('position', 'fixed');
				$('#contizq').css('top', yizq+"px");
				if (tizq > 1) { 
					hide('#i'+(tizq-1));
				}
				$(window).unbind('scroll');	
				$(window).bind('scroll', function(event){
					ty = $(document).scrollTop();
					
					if (ty <= yini) {
						$('#contizq').css('top', (yizq-ty)+'px');
						if (tizq > 1) hide('#i'+(tizq-1));
					} else {
						if (tizq == 1) {
							$('#contizq').css('top', (yizq-ty)+'px'); 	
						} else {
							if (ty < yini+(tizq-1)*561) {
								$('#contizq').css('top', parseInt(yizq-yini*2+ty) + 'px');
								show('#i'+(tizq-1));
								if (tizq > 1) show('#i2');
								if (tizq > 2) show('#i3');
							} else {
								$('#contizq').css('top', parseInt(yini+(tizq-1)*561-ty+85) + 'px');
								if (tizq > 1) hide('#i2');
								if (tizq > 2) hide('#i3');
							}
						}
					}
				});
			} else {
				$(window).unbind('scroll');
				$('#contizq').css('position', 'static');
			}
		}
		$('#contizq').fadeIn(350);
		$('#contder').fadeIn(350);
	}
	// contact
	$('#contact').css('height', $(document).height()+'px');
	$('#contact').click(function(){
		$('#contact').fadeOut("slow");
		$('#menu').fadeIn("slow");
		return false;
	});
	$('.image').click(function(){ return false; });
	$('.image a').click(function(){ window.location = $(this).attr('href'); return false; });	
	
	var loadCat = function(cat) {
		categ = cat;
		$('#contizq').fadeOut(350);
		$('#contder').fadeOut(350);
		setTimeout(renderCat, 350);
	}
	var renderCat = function() {
		$.getJSON('json.data.php?cat='+categ, function(data) {
			var middle = Math.round(data.length/2);
			var itemL = [];
			var itemR = [];
			for (i=0; i<data.length; i++) {
				var pos = i+1;
				if (i>=middle) pos = i+1-middle;
				var titem = "";
				titem = '<div class="item box'+data[i].template+' cat'+data[i].category+'" id="i'+pos+'">';
				titem+= '<div class="body" onclick="showPop(\''+data[i].image+'\');">';
				titem+= '<h2>'+data[i].title+'</h2>';
				switch (data[i].type) {
					case "jpg": 
						titem+= '<div style="padding-top: '+data[i].margin+'px;"><img src="cms/files/thb_'+data[i].image+'" style="max-height: '+data[i].h+'px" /></div>';
						break;
					case "gif":
						titem+= '<div style="padding-top: '+data[i].margin+'px;"><img src="cms/files/'+data[i].image+'" style="max-height: '+data[i].h+'px" /></div>';
						break;
					case "swf":
						titem+= '<div style="padding-top: '+data[i].margin+'px;">';
						titem+= '<object type="application/x-shockwave-flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+data[i].w+'" height="'+data[i].h+'">'; 
        				titem+= '<param name="src" value="cms/files/'+data[i].image+'">';
						titem+= '<param name="wmode" value="transparent">';
        				titem+= '<embed src="cms/files/'+data[i].image+'" type="application/x-shockwave-flash" width="'+data[i].w+'" height="'+data[i].h+'" wmode="transparent"></embed>';
    					titem+= '</object>';
						titem+= '</div>';
						break;
				}
				titem+= '</div>';
				titem+= '<div class="foot">';
				titem+= data[i].content;
				if (data[i].url != "") {
					titem+= '<div class="link"><img src="img/arrow.png" width="9" height="7" /> <strong>link</strong> <a href="'+data[i].url+'" target="_blank">'+data[i].url+'</a></div>'
				} else {
					titem+= '<div class="link">&nbsp;</div>'
				}
				titem+= '</div>';
				titem+= '</div>';
				if (i<middle) {
					itemL.push(titem);
				} else {
					itemR.push(titem);
				}
			}
			$('.item').remove();
 			$(itemL.join('')).appendTo('#contizq');
			$(itemR.join('')).appendTo('#contder');
			renderFoots();
			// apply parallax
			setTimeout(parallax, 350)
		});
	}
	loadCat(0);
});
function showPop(img) {
	if (categ == 4) {
		$.fancybox({'href' : '/cms/files/'+img });
	}
}
