/*
 * jQuery css clip animation support -- Jim Palmer
 * version 0.1.2
 * idea spawned from jquery.color.js by John Resig
 * Released under the MIT license.
 */

(function(jQuery){
	/*jQuery.fn.image = function(src, f){ 
		return this.each(function(){ 
			var i = new Image(); 
			i.src = src; 
			i.onload = f; 
			this.appendChild(i);
		}); 
	}*/
    jQuery.fx.step.clip = function(fx){
        if ( fx.state == 0 ) {
            var cRE = /rect\(([0-9]{1,})(px|em)[,]?\s?([0-9]{1,})(px|em)[,]?\s?([0-9]{1,})(px|em)[,]?\s?([0-9]{1,})(px|em)\)/;
            fx.start = cRE.exec( fx.elem.style.clip.replace(/,/g, '') );
			if( fx.end && cRE && fx.end.replace )
            	fx.end = cRE.exec( fx.end.replace(/,/g, '') );
        }
        
        if( !fx.start || !fx.end ) return false;
        var sarr = new Array(), earr = new Array(), spos = fx.start.length, epos = fx.end.length,
            emOffset = fx.start[ss+1] == 'em' ? ( parseInt($(fx.elem).css('fontSize')) * 1.333 * parseInt(fx.start[ss]) ) : 1;
        
        for ( var ss = 1; ss < spos; ss+=2 ) { sarr.push( parseInt( emOffset * fx.start[ss] ) ); }
        for ( var es = 1; es < epos; es+=2 ) { earr.push( parseInt( emOffset * fx.end[es] ) ); }
        fx.elem.style.clip = 'rect(' +
            parseInt( ( fx.pos * ( earr[0] - sarr[0] ) ) + sarr[0] ) + 'px ' +
            parseInt( ( fx.pos * ( earr[1] - sarr[1] ) ) + sarr[1] ) + 'px ' +
            parseInt( ( fx.pos * ( earr[2] - sarr[2] ) ) + sarr[2] ) + 'px ' +
            parseInt( ( fx.pos * ( earr[3] - sarr[3] ) ) + sarr[3] ) + 'px)';
    }
})(jQuery);

(function(){
// Slideshow effect By SMartWD <SmartWD.com>
var Toint = function( css ) {
	return Number(css.substr(0,css.length-2));
}
var Pictures = new Array();// Pictures Array
// SlideShow library( class ), based on jQuery
var SlideShow = window.SlideShow = {
	Wrapper: null,
	Pictures: new Array(),
	Current: null,
	Count: 0,
	Side: "right",
	Current_number: 0,
	Padding: 0,
	Text: null,
	Timer: 0,
	Init: function( Id ) {
		SlideShow.Wrapper = $("#"+Id);
		SlideShow.Pictures = SlideShow.Wrapper.find(".sspe");
		SlideShow.Current = SlideShow.Wrapper.find("#slide_show_current");
		SlideShow.Text = SlideShow.Wrapper.find("#slide_show_text");
		this.Current_number = this.Pictures.length - 1;
		this.Padding = SlideShow.Wrapper.position().left + Toint(SlideShow.Wrapper.css("margin-left"));
		var _self = this;
		SlideShow.Pictures.each(function(k,v){
			$(v).css("left", _self.Padding+"px");
			_self.Padding += 17;
		});
		SlideShow.Wrapper.css("padding-left", this.Padding - (SlideShow.Wrapper.position().left + Toint(SlideShow.Wrapper.css("margin-left"))) +"px");
		this.Padding = 0;
		this.SetText();
		$(window).load(function(){SlideShow.Timer = window.setInterval( "SlideShow.Step()", 5500 );});
		
	},
	Step: function() {
		// Refresh the library
		SlideShow.Current = SlideShow.Wrapper.find("#slide_show_current");
		var Left = this.Wrapper.position().left + Toint(this.Wrapper.css("margin-left"));
		var Right = Left + this.Wrapper.width();
		// Check for end 
		if( this.Current_number < 0 || this.Current_number == this.Pictures.length ) {
			this.Side = (this.Side == "right") ? "left" : "right";
			if( this.Side == "left" )
				this.Current_number = 0;
			else
				this.Current_number = this.Pictures.length - 1;
			return false;
		}
		// Get the next one to open
		var Next = $(this.Pictures[this.Current_number]);
		if( this.Side == "right" ) {
		// Minimize the old picture
		this.Current.css("position", "absolute").css("clip", "rect(0px 1000px 1000px 0px)");
		this.Current.animate({"clip":"rect(0px "+this.Current.width()+"px 1000px "+(this.Current.width() - 15)+"px)"}, 700);
		// Remove left padding
		this.Wrapper.animate({paddingLeft: (Toint(this.Wrapper.css("padding-left"))-17)+"px"}, 300);
		// Add right padding
		this.Wrapper.animate({paddingRight: (Toint(this.Wrapper.css("padding-right"))+17)+"px"}, 300, null, function(){SlideShow.SetText();});
		// Move to correct place
		this.Current.css("left", (Left + Toint(this.Wrapper.css("padding-left")))+"px");
		// Maximize the next one
		Next.css("clip", "rect(0px 15px 1000px 0px)");
		Next.animate({"clip":"rect(0px 1450px 1000px 0px)"}, 400);
		// Change information
		Next.attr("id","slide_show_current");
		Next.removeClass("slide_show_picture");
		Next.removeClass("sspe");
		this.Current.attr("id", "");
		this.Current.addClass("sspe");
		//this.SetText();
		this.Pictures[this.Current_number] = this.Current;
		this.Current_number--;
		} else {
			this.Current.css("position", "absolute").css("clip", "rect(0px 1000px 1000px 0px)");
			this.Current.animate({"clip":"rect(0px 15px 1000px 0px)"}, 700);
			// Remove left padding
			this.Wrapper.animate({paddingRight: (Toint(this.Wrapper.css("padding-right"))-17)+"px"}, 300);
			//// Add right padding
			this.Wrapper.animate({paddingLeft: (Toint(this.Wrapper.css("padding-left"))+17)+"px"}, 300, null, function(){SlideShow.SetText();});
			// Move to correct place
			this.Current.css("left", (Left + Toint(this.Wrapper.css("padding-left")))+"px");
			// Maximize the next one
			Next.animate({"clip":"rect(0px 1450px 1000px 0px)"}, 400);
			// Change information
			Next.attr("id","slide_show_current");
			Next.removeClass("slide_show_picture");
			Next.removeClass("sspe");
			this.Current.attr("id", "");
			this.Current.addClass("sspe");
			// Move text
			this.Pictures[this.Current_number] = this.Current;
			this.Current_number++;	
		}
	},
	SetText: function() {
		this.Text.find("span").html("+ "+SlideShow.Wrapper.find("#slide_show_current").attr("alt"));
		var Top = this.Wrapper.position().top + this.Wrapper.height();
		if( this.Wrapper.css("margin-top")!= "auto" )
			Top += Toint(this.Wrapper.css("margin-top"));
		var Left = 0;
		Left = this.Wrapper.position().left;
		if( this.Wrapper.css("padding-left")!= "auto" )
			Left += Toint(this.Wrapper.css("padding-left"));
		this.Text.css({"position":"absolute","top":Top+"px", "left":Left+"px"});
		
	},
	Pause: function( btn ) {
		window.clearInterval( this.Timer );
		$(btn).find("img").attr("src", "images/slideshow/site_button_play.png");
		btn.onclick = function(){return SlideShow.Play(this);};
		return false;
	},
	Play: function( btn ) {
		this.Timer = window.setInterval( "SlideShow.Step()", 6000 );
		$(btn).find("img").attr("src", "images/slideshow/site_button_pause.png");
		btn.onclick = function(){return SlideShow.Pause(this);};
	}
};	
// SlideShow code end
$(document).ready(function(){
	if( $("#slide_show")[0] )
		SlideShow.Init( "slide_show" );
});
})();


//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// Mini gallery effect ( By SMartWD <SmartWD.com> )
/**
 * Mini gallery library
 * 
 * @version V1.0
 */
var Mini_Gallery = {
	/**
	 * Current page in the gallery
	 *
	 * @var Number
	 */
	Current_page: 0,
	
	/**
	 * Current picture in the gallery
	 *
	 * @var Number
	 */
	Current_pic: 0,
	
	/**
	 * Document loading status
	 *
	 * @var String( loaded , ready , no )
	 */
	Document_State: "no",
	
	/**
	 * Timer id
	 *
	 * @var Number
	 */
	Timer: 0,
	
	/** 
	 * initialize the mini-gallery
	 *
	 * @return void
	 */
	Init: function() {
		$(document).ready(function(){
			Mini_Gallery.Document_State = "ready";
			// Pages scrolling
			$("div#ohad_site_main_content_left_img").scrollable({
				size: 1,
				items: '#thumbs_pages'
			});
			// Pictures scrolling
			$("div#big_picture_holder").scrollable({
				size: 1,
				items: '#big_picture_slider'
			});
			// Zoom button
			$("#zoom_button__").lightBox();
			
			$(".__zoom_picture__").each(function(k,e){
				$(e).lightBox();
			});
			$("#thumbs_pages img , #big_picture_slider img").saveImage();
		});
		$(window).load( $.imageManager.run );
		$(window).bind( "imageload" , function(evt , args){
			var total = $.imageManager.images.length;
			var percent = Math.floor((args.index / total) * 100);
			$("#loading_percent").html( percent );
			$("#loading_bar").css("width" , percent+"%");
		});
		$(window).bind( "imagesloaded" , function(){
			// Display all
			$("#__temp_loading__").hide("slow");
			$("#ohad_site_main_content_page_content").show(1500);
			$("#ohad_site_main_content_left_img").show(1500);
			// Tell the page loading status
			Mini_Gallery.Document_State = "loaded";
			// Black&White intialise
			$("div.thumbnail").each( function( k , v ) {
				$(v).children(0).pixastic( 'desaturate' );
				$(v).click(function(){ Mini_Gallery.Pause(); Mini_Gallery.SetPicture( k ); });
				$(v).css("cursor", "pointer");
				v.k = k;
			});
			// Thumbnail hover
			$("div.thumbnail").hover(
				function(){
					if( Mini_Gallery.Current_pic == this.k ) return false;
					Mini_Gallery.highLight( this , false );
				 },
				function(){
					if( Mini_Gallery.Current_pic == this.k ) return false;
					Mini_Gallery.highLight( this , true );
				}
			);
			// First initialise
			Mini_Gallery.SetPicture( 0 );
			// Auto move
			Mini_Gallery.Play();
			
			$("#big_picture_slider div").each(function(k,v){
				var padding = 270-($(v).find("img").width()/2);
				$(v).css("padding-left" , padding+"px" );
				$(v).css("width" , (540 - padding) + "px" );
			});
		});
	},
	
	/** 
	 * Resume the mini-gallery slideing
	 *
	 * @return void
	 */
	Play: function() {
		this.Timer = window.setInterval(function(){Mini_Gallery.NextPic();},6000);
		var html = "<a href=\"javascript:Mini_Gallery.Pause()\" style=\"color:#ffffff;\"><img src=\"images/site_gallery/button_stop.png\" alt=\"\" border=\"0\" style=\"vertical-align:middle\" /> <span class=\"slideshow_links\">עצור מצגת</span></a>";
		$("#ohad_site_main_content_gray_box_text").html( html );
	},
	
	/** 
	 * Pause the mini-galery slideing
	 *
	 * @return void
	 */
	Pause: function() {
		clearInterval( this.Timer );
		var html = "<a href=\"javascript:Mini_Gallery.Play()\" style=\"color:#ffffff;\"><img src=\"images/site_gallery/button_play.png\" alt=\"\" border=\"0\" style=\"vertical-align:middle\" /> <span class=\"slideshow_links\">הפעל מצגת</span></a>";
		$("#ohad_site_main_content_gray_box_text").html( html );
	},
	
	/** 
	 * Move to the next page
	 *
	 * @return void
	 */
	NextPage: function() {
		this.Pause();
		var api = $("div#ohad_site_main_content_left_img").scrollable(); 
		api.next(); 
		this.SetPicture( api.getPageIndex() * 12 );
		this.updatePage();
	},
	
	/** 
	 * Move to the next picture
	 *
	 * @return void
	 */
	NextPic: function() {
		if( this.Current_pic == ($("#big_picture_slider").children().length - 1) )
			this.SetPicture( 0 );
		else
			this.SetPicture( this.Current_pic + 1 );
	},
	
	/** 
	 * Move to the previous page
	 *
	 * @return void
	 */
	PrevPage: function() {
		this.Pause();
		var api = $("div#ohad_site_main_content_left_img").scrollable(); 
		api.prev(); 
		this.SetPicture( api.getPageIndex() * 12 );
		this.updatePage();
	},
	
	/** 
	 * Move to the previous picture
	 *
	 * @return void
	 */
	PrevPic: function() {
		if( this.Current_pic == 0 )
			this.SetPicture( ($("#big_picture_slider").children().length - 1) );
		else
			this.SetPicture( this.Current_pic - 1 );
	},
	
	/** 
	 * Set the big-picture title
	 *
	 * @param title String
	 * @return void
	 */
	SetTitle: function( title ) {
		var api = $("div#big_picture_holder").scrollable();
		$(".big_picture_title").html((api.getPageIndex()+1)+"/"+api.getPageAmount()+" "+title);
		$("#zoom_button__").attr("title", $(".big_picture:eq("+this.Current_pic+") img").attr("alt"));
	},
	
	/** 
	 * Set the big-picture
	 *
	 * @param sel String
	 * @return void
	 */
	SetPicture: function( index ) {
		if( $(".thumbnail:eq("+this.Current_pic+")")[0] )
			this.highLight( this.Current_pic , true );
		var api = $("div#ohad_site_main_content_left_img").scrollable();
		api.seekTo( Math.floor($(".thumbnail:eq("+index+")")[0].k / 12) );
		this.Current_pic = index;
		api = $("div#big_picture_holder").scrollable();
		api.seekTo( index );
		this.SetTitle( $(".big_picture:eq("+this.Current_pic+")").attr("alt"));
		$("#zoom_button__").attr("href",$(".big_picture:eq("+this.Current_pic+")").attr("src") );
		this.updatePage(); 
		this.highLight( index );
	},
	
	/** 
	 * highlight the current picture in the pages table
	 *
	 * @param index String
	 * @param un Boolen
	 * @return void
	 */
	highLight: function( index, un ) {
		var sel = typeof index == "number" ? ".thumbnail:eq("+index+")" : index;
		if( !un ) {
			Pixastic.revert( $(sel).children(0).get(0) );
			$(sel).css("border", "solid 3px #FFF");
		} else {
			$(sel).children(0).pixastic( 'desaturate' );
			$(sel).css("border", "solid 3px #666666");
		}
	},
	
	/** 
	 * Set the thumbnails page
	 *
	 * @return void
	 */
	updatePage: function() {
		var api = $("div#ohad_site_main_content_left_img").scrollable(); 
		$("#page_index").html("+ "+(api.getPageIndex()+1)+"/"+api.getPageAmount());
		api = $("div#big_picture_holder").scrollable();
		$("#ohad_site_main_content_numbers").html((api.getPageIndex()+1)+" / "+(api.getPageAmount()-1));
	}
	
};

