/**
 * ArtikDesign 2011 ©
 * Default scripts file - Interface/JScripts.css
 * Wrote by NetanelEdri ( WEB Developer ) - www.NetanelEdri.net
**/

// Images Slideshow Plugin
(function($){
	$.fn.TemporaryImages = function (options){
		var defaultOptions = {
			interval : 2000,
			speed    : 1000,
			effect   : true
		};
	
		var opt = $.extend(defaultOptions, options);
		return this.each(function(){
			var element = $(this);
			element.addClass("TemporaryImages");	
			setInterval(function(){
				var active  = element.children("img.active");
				var next;
				if(active.length == 0 ) 
					active = element.children("img:last");
				if(active.next().length)
					next = active.next();
				else
					next = element.children("img:first");
				
				active.addClass('last-active');
				next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0},opt.speed, function(){
					active.removeClass('active last-active');
				});
			},opt.interval);
		});
	};
})(jQuery);

$(window).load(function(){
	$("#project-big-image img").each(function(){
		var width = (parseInt($(this).width())  / 2) * -1;
		$(this).css({
			left : "50%",
			marginLeft : width + "px"
		});
	});
});

$(document).ready(function(){
	var $dialog = $("#dialog").dialog({
		autoOpen: false,
		title: 'Jobs offers',
		modal : true,
		width : 450
	});

	$('a.modal-link').click(function() {
		$dialog.dialog('open');
		// prevent the default action, e.g., following a link
		return false;
	});
	
    if(document.images)
    {
      preload_image_object = new Image();
      image_url = new Array();
      image_url[0] = "Interface/images/arrow_up.png";
      image_url[1] = "Interface/images/arrow_up_black.png";
      image_url[2] = "Interface/images/arrow_down.png";
      image_url[3] = "Interface/images/arrow_down_black.png";
      image_url[4] = "Interface/images/next_project.png";
      image_url[5] = "Interface/images/previous_project.png";
      image_url[6] = "Interface/images/arrow_right.png";
      image_url[7] = "Interface/images/arrow_left.png";

       var i = 0;
       for(i=0; i<=3; i++) 
         preload_image_object.src = image_url[i];
    }
	
	var menuPos = $("#menuPos").attr("rel") - 1;
	$("#menu ul li").eq(menuPos).children("a").css("fontWeight","bold");
	
	$("label").each(function(){
		$(this).attr("for",$(this).next().attr("id"));
	});
	
	/*$(".project-images-single").click(function(){
		var x = $("#project-big-image img.active").index();
		var y = $(this).index();
		$("#project-big-image img").eq(x).animate({opacity:0.0},1200,function(){
			$("#project-big-image img").eq(x).css("-moz-opacity","0");
			$("#project-big-image img").eq(x).css("-webkit-opacity","0");
			$("#project-big-image img").eq(x).css("opacity","0.0");
			$("#project-big-image img").eq(x).removeClass("active");
			$("#project-big-image img").eq(y).animate({opacity:1.0},1300,function(){
				$(this).addClass("active");
			});
		});
	});*/
	
	$("#project-big-image img").each(function(){
		var height = parseInt($(this).height());
		var width = parseInt($(this).width())  / 2 * -1;
		$(this).css({
			left : "50%",
			marginLeft : width + "px"
		});
	});
	
	$("#project-big-image img.active").fadeIn(1500);
	
	$(".project-images-single").click(function(){
		var x = $("#project-big-image img.active").index();
		var y = $(this).index();
		$("#project-big-image img").eq(x).fadeOut(1500,function(){
			$("#project-big-image img").eq(x).removeClass("active");
			$("#project-big-image img").eq(y).fadeIn(1200,function(){
				$(this).addClass("active");
			});
		});
	});
	
	$("#project-big-image img").click(function(){
		if($(this).next().length){
			$(this).fadeOut(1500,function(){
				$(this).removeClass("active");
				$(this).next("img").fadeIn(1200,function(){
					$(this).addClass("active");
				});
			});
		}else {
			$(this).fadeOut(1500,function(){
				$(this).removeClass("active");
				$("#project-big-image img:first").fadeIn(1200,function(){
					$(this).addClass("active");
				});
			});			
		}
	});
	
	/*$("#project-big-image img").click(function(){
		if($(this).next().length){
			$(this).animate({opacity:0.0},1200,function(){
				$(this).removeClass("active");
				$(this).next("img").animate({opacity:1.0},1300,function(){
					$(this).addClass("active");
				});
			});
		}else {
			$(this).animate({opacity:0.0},1200,function(){
				$(this).removeClass("active");
				$("#project-big-image img:first").animate({opacity:1.0},1300,function(){
					$(this).addClass("active");
				});
			});			
		}
	});*/
	

	$("a.move-project").hover(function(){
		$(this).children("img").attr("src","Interface/images/" + $(this).children("img").attr("rel"));
	},function(){
		if($(this).attr("id") == "next-project"){
			$(this).children("img").attr("src","Interface/images/next_project.png");
		}else {
			$(this).children("img").attr("src","Interface/images/prev_project.png");
		}
	});
	
	$("a.move-project").click(function(){
		var a = $("a.active-project-link");
		var index = a.index();
		var l = $("#cat-slider a").length;
		if($(this).attr("id") == "next-project"){
			if(index + 1 == l){
				return false;
			}else {
				var u = $("#cat-slider a:eq("+(index + 1)+")").attr("href");
				window.document.location = u;
			}
		}else {
			if($("#cat-slider a:eq("+(index - 1)+")").attr("href").toString() != "undefined"){
				var u = $("#cat-slider a:eq("+(index - 1)+")").attr("href");
				window.document.location = u;
			}
		}
	});

	$("a#Arrow-Up").hover(function(){
		$(this).children("img").attr("src","Interface/images/arrow_up_black.png");
	},function(){
		$(this).children("img").attr("src","Interface/images/arrow_up.png");
	});
	
	$("a#Arrow-Down").hover(function(){
		$(this).children("img").attr("src","Interface/images/arrow_down_black.png");
	},function(){
		$(this).children("img").attr("src","Interface/images/arrow_down.png");
	});
	
	$("#cat-slider a").not(".active-project-link").children("span").hide();
	
	$("#cat-slider a").not(".active-project-link").bind("mouseover",function(){
		$(this).children("span").show();
	});
	
	$("#cat-slider a").not(".active-project-link").bind("mouseout",function(){
		$(this).children("span").hide();
	});
	
    $("a#Arrow-Up").hover(function(){$("#cat-slider").animate({scrollTop: 0},5000);},function(){$("#cat-slider").stop();});
    $("a#Arrow-Up").click(function(){$("#cat-slider").animate({scrollTop: 0});},function(){$("#cat-slider").stop();});
    $("a#Arrow-Down").hover(function(){$('#cat-slider').animate({scrollTop: $("#cat-slider")[0].scrollHeight},5000);},function(){$("#cat-slider").stop();});
    $("a#Arrow-Down").click(function(){$("#cat-slider").animate({scrollTop: $("#cat-slider")[0].scrollHeight});},function(){$("#cat-slider").stop();});	
	
	$("input.contact-input:first").focus();
	$("#home-banner").TemporaryImages({interval : 3000,speed : 1500});
	$("#menu ul li").each(function(){
		$(this).css("width",$(this).width() + 17 + "px");
	});
	$(".home-cat a").hover(function(){
		$(this).parent().addClass("gray-arrow");
	},function(){
		$(this).parent().removeClass("gray-arrow");
	});
	if ($.browser.msie  && parseInt($.browser.version) == 7) {
		$("#menu").css("marginLeft","0px");
	}
	
	$("form#form-1").submit(function(){
		$.ajax({
			url : "ajax.php?act=contact",
			type : "POST",
			data : $(this).serialize(),
			success : function(m){
				if(m != "OK"){
					alert(m);
				}else {
					alert("Request is sent successfully !");
					$("form#form-1 input,form#form-1 textatea").not(":submit").val("");
				}
			}
		});
		return false;
	});
});
