// JavaScript Document

/* Define iOS devices */
jQuery.extend(jQuery.browser,
	{SafariMobile : navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i) }
);

// form-search
$(function () {
	$('.int').focus(function () {
		$(this).parent().addClass('i-f');}).blur(function () {
			$(this).parent().removeClass('i-f')
		});
});

/* Hide hampage overlay mast for iOS device */
$(function() {
	if ($.browser.SafariMobile){
		$("#big-mask").css("display", "none");
	}
});

//footer
/*$(function () {
	$(window).resize(function () {
		h = $(window).height() - $("#top_bg").height() - $("#content").height();
		$("#content").css('min-height', h+"px");
	});
	$(window).trigger("resize");
});*/

/* Create masonry layout with Isotope (http://isotope.metafizzy.co/) */

// Masonry for regular POSTS
$(function(){
	var $multicol = $('#multicol');
	if (!$multicol.length)
		return;
	$multicol.addClass('isotoped');
	$multicol.isotope({
		itemSelector : '#multicol .article_box',
		transformsEnabled: false,
		animationEngine: 'jquery',
		masonry : {
			columnWidth : 280
		},
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false
		}
	});
});

// Masonry for PHOTOS and ALBUMS
$(function(){
	var $multicol = $('#multicol-gal');
	if (!$multicol.length)
		return;
	$multicol.addClass('isotoped');
	$multicol.isotope({
		itemSelector : '#multicol-gal .gallery-box',
		transformsEnabled: false,
		masonry : {
			columnWidth : 280
		},
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false
		}
	});

});


/* Swap featured image by vector objects of slashed shape */
$(document).ready(function() {

	//Add rollovers to post featured image
	$('.img-holder').append('<span class="zoom"></span>').each(function () {
		var $span = $('> span.zoom', this);
		if ($.browser.msie && $.browser.version < 9)
			$span.hide();
		else
			$span.css('opacity', 0);
		$(this).hover(function () {
			if ($.browser.msie && $.browser.version < 9)
				$span.show();
			else
				$span.stop().fadeTo(1000, 1);
		}, function () {
			if ($.browser.msie && $.browser.version < 9)
				$span.hide();
			else
				$span.stop().fadeTo(1000, 0);
		});
	});

	// Swap featured images with vector objects
	$(".img-holder").each(function () {
		var img = $(this).find("img");
		var img_w = img.attr("width");
		var img_h = img.attr("height");
		var img_h_m = img_h - 30;
		var img_src = img.attr("src");
		img.css('display' , 'none');

		var paper = Raphael(this, img_w, img_h);
		// iOS devices can not apply path background image correctly. We are using clipping instead.
		if ($.browser.SafariMobile){
			var p = "M0,30L"+img_w+",0L"+img_w+","+img_h_m+"L0,"+img_h+"L0,30";
			var p_img = paper.image(img_src, 0, 0, img_w, img_h);
			p_img.attr({
				stroke: "none",
				"clip-rect": p
			});
		}
		else{
			var c = paper.path("M 0 30   L "+img_w+" 0   L "+img_w+" "+img_h_m+"   L 0 "+img_h+"   L 0 30");
			c.attr({
				stroke: "none",
				fill: "url("+img_src+")"
			});
		}
	});

	// Add rollovers to gallery thumbnails
	$(".img-holder"+($.browser.msie ? ":not(.n-s)" : "")+" div").each(function () { 
		var img_w = $(this).parent("div").width();
		if ($.browser.msie)
	        var img_h = $(this).parent("div").height() + 1;
		else
			var img_h = $(this).parent("div").height() + 2;
		var img_h_m = img_h - 30;

		var paper = Raphael(this, img_w, img_h);
		var c = paper.path("M 0 30   L "+img_w+" 0   L "+img_w+" "+img_h_m+"   L 0 "+img_h+"   L 0 30");
		c.attr({
			stroke: "none",
			fill: "url(images/gal-h.png)"
		});
	});

	// Swap HomePage thumbnails with vector objects
	$(".slide-h").each(function () {
		var img_w = $(this).find("img").attr("width");
		var img_h = $(this).find("img").attr("height");
		var img_h_m = img_h - 15;
		var img_src = $(this).find("img").attr("src");

		var paper = Raphael(this, img_w, img_h);

		if ($.browser.SafariMobile){
			var p = "M0,15L"+img_w+",0L"+img_w+","+img_h_m+"L0,"+img_h+"L0,15";
			var img = paper.image(img_src, 0, 0, img_w, img_h);
			img.attr({
				stroke: "none",
				"clip-rect": p
			});
		}
		else{
			var c = paper.path("M 0 15   L "+img_w+" 0   L "+img_w+" "+img_h_m+"   L 0 "+img_h+"   L 0 15");
			c.attr({
				stroke: "none",
				fill: "url("+img_src+")"
			});
		}
	});

});


/* Service tooltips: tags, categories, social links, etc. */
$(function() {
	$('.ico-l').each(function () {
		var $tip = $('> .info-block', this);
		var $span = $('> span', this);
		if (!$span.length) {
			var $span = $('> a', this);
		};
		var $old_html = $tip.html();
		var $new_html = '<div class="box-i-l"><div class="box-i-r"><span class="box-i">' + $tip.html() + '</span></div></div>';
		$(this).hover(function () {
			$span.addClass('act');
			$tip.html($new_html);			
			if ($.browser.msie && $.browser.version < 9) {
				$tip.show();
			} else {
				$tip.css('display', 'none');
				$tip.stop().fadeTo(300, 1);}
		}, function () {
			$span.removeClass('act');
			if ($.browser.msie && $.browser.version < 9) {
				$tip.hide();
				$tip.html($old_html);
			} else {
				$tip.css('display', 'block');
				$tip.stop().fadeTo(50, 0, function() {$tip.css('display', 'none'); $tip.html($old_html);});
			}
		});
	});
});



/* New gallery */
$(function () {

   // normal hs
   $(".fadeThis").each(function () {
      if ( $(this).attr("href") == '#' )
         $(this).attr("href", $(this).find("img").attr("src") );
      this.onclick = function () {
         hs.expand(this, hs_config2)
         return false;
      };
   });

   // gallery 1 level 
   $(".one_level_gal").each(function () {
      els = $(".img-holder", this);
      els.each(function () {
         var config = {};
         $.extend(config, hs_config1);
         var im = $(this).children("img");
         var s = im.attr("src");
         //config.src = s;
         config.slideshowGroup = slideshow_options.slideshowGroup;
         var mini = $(this).children("a");
         if ( mini.length )
         {
            //config.src = mini.attr("href");
         }
         else
         {
            mini = $("<a />");
            mini.attr("href", s);
            mini.appendTo( $(this) );
         }
         
         mini.append( im.clone() );
         
         mini[0].onclick = function () {      
            return hs.expand(this, config);
         };
         
         $(this).click(function () {
            mini.trigger("onclick");
            return false;
         });
      });
   });
   
});




//jQuery.noConflict();
jQuery(document).ready(function($){
	
/* Quick Isotope for Gallery */
function do_isotope(iContainer, iItem, iWidth) {
	var $iContainer = $(iContainer);
	if (!iContainer.length)
		return;
	$iContainer.isotope({
		containerClass : 'isotoped',
		itemSelector : iItem,
		transformsEnabled: false,
		masonry : {
			columnWidth : iWidth
		},
		animationOptions: {
			duration: 750,
			easing: 'linear',
			queue: false
		}
	});
	//console.log(iContainer +' isotoped');
}
/*--------------------------------------------------*/

/*  Slasher for Gallery  */
function do_slash(iItem) {
	// Swap featured images with vector objects
	$(iItem).each(function () {
		var img = $(this).find("img");
		var img_w = img.attr("width");
		var img_h = img.attr("height");
		var img_h_m = img_h - 30;
		var img_src = img.attr("src");
		
		$(this).css('height',img_h);
		$(this).css('widtht',img_w);
		img.css('display' , 'none');

		var paper = Raphael(this, img_w, img_h);
		// iOS devices can not apply path background image correctly. We are using clipping instead.
		if ($.browser.SafariMobile){
			var p = "M0,30L"+img_w+",0L"+img_w+","+img_h_m+"L0,"+img_h+"L0,30";
			var p_img = paper.image(img_src, 0, 0, img_w, img_h);
			p_img.attr({
				stroke: "none",
				"clip-rect": p
			});
		}
		else{
			var c = paper.path("M 0 30   L "+img_w+" 0   L "+img_w+" "+img_h_m+"   L 0 "+img_h+"   L 0 30");
			c.attr({
				stroke: "none",
				fill: "url("+img_src+")"
			});
		}
	});
}
/*--------------------------------------------------*/

/* Overlay for gallery */
function build_overlay (iItem) {
	// Add rollovers to gallery thumbnails
	$(iItem).each(function () { 
		var img_w = $(this).parent('div').width();
		if ($.browser.msie)
			var img_h = $(this).parent('div').height() + 1;
		else
			var img_h = $(this).parent('div').height() + 2;
		var img_h_m = img_h - 30;

		var paper = Raphael(this, img_w, img_h);
		var c = paper.path("M 0 30   L "+img_w+" 0   L "+img_w+" "+img_h_m+"   L 0 "+img_h+"   L 0 30");
		c.attr({
			stroke: "none",
			fill: "url(images/gal-h.png)"
		});
	});
	$(iItem).append('<span class="zoom"></span>').each(function () {
		var $span = $(this);
		if ($.browser.msie && $.browser.version < 9)
			$span.hide();
		else
			$span.css('opacity', 0);
		$(this).parent('div').hover(function () {
			if ($.browser.msie && $.browser.version < 9)
				$span.show();
			else
				$span.stop().fadeTo(300, 1);
		}, function () {
			if ($.browser.msie && $.browser.version < 9)
				$span.hide();
			else
				$span.stop().fadeTo(300, 0);
		});
	});
}
/*--------------------------------------------------*/

/* zGallery */
var in_work = 0;
$('.two_level_gal .img-holder').click(function() {
	
	if (in_work > 0){
		return false;
	}
		
	in_work = 1;
	
	var cl = $(this).parents(".gallery-box").attr("class").replace(/^.*link_(gal[0-9]+).*$/, 'for_$1');
	var photos = $('.' + cl).clone();

	if (!photos.length){
		alert('Error. Album "'+cl+'" is empty.');
		in_work = 0;
		return false;
	}

	var h2 = photos.children('strong').detach().html();
	var old_html = photos.html();

	photos.find("a").each(function() {
		var href = $(this).attr('href');
		var src = $(this).attr('data-src');
		var width = $(this).attr('data-width');
		var height = $(this).attr('data-height');
		var caption = $(this).text();

		var new_html = '<div class="gallery-box"><div class="overlay"><a href="'+ href +'" title="'+ caption +'"><img src="'+ src +'" width="'+ width +'" height="'+ height +'" alt="" /></a></div></div>';

		$(this).replaceWith(new_html);
	});
		
	var new_html = '<div class="holder"><a class="button" href="#"><span class="but-r"><i class="back"></i>BACK</span></a><h2 class="cufon_me">' + h2 + '</h2><div class="multicol-gallery">' + photos.html() + '</div><div class="but-bottom"><a class="button bot" href="#"><span class="but-r"><i class="back"></i>BACK</span></a></div></div>';

	var gallery = $('<div class="bg '+ cl +'" />');
	gallery.html(new_html).appendTo('body').addClass('hidden').addClass('gal-full');
	
	Cufon.refresh(); //undefined selector. this may slow down site.
	do_slash('.bg .gallery-box');
	build_overlay('.bg .gallery-box .overlay');
	var elems = gallery.find(".gallery-box");

	elems.each(function () {
		$(this).addClass("hs_attached");
		var link = $(this).find('a');

		link[0].onclick = function () {
			return hs.expand(this, slideshow_albums);
		};

		$(this).click(function () {
			$(link).trigger("onclick");
			return false;
		});
	});

	var prev_scroll_top = $(window).scrollTop();
	
	$('html:not(:animated)'+( ! $.browser.opera ? ',body:not(:animated)' : '')).animate({scrollTop: 0}, 500, function () {

		$(".holder", gallery).css({
			visibility: 'visible',
			opacity: 0,
			'z-index': 999
		});

		$('#bg').css({
			position: 'fixed',
			top: 0,
			width: '100%'
		});

		gallery.css({
			display: 'block',
			opacity: 0,
			visibility: 'visible'
		}).animate({
			opacity: 1
		}, {
			queue: false,
			duration: 1000,
			complete: function (){

				do_isotope('.bg .multicol-gallery' , '.bg .gallery-box', 460);

				$(".holder", gallery).animate({
					opacity: 1
				}, {
					queue: false,
					duration: 1000,
					complete: function () {
						$("a.button").unbind('click').click(function () {
							$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollTop: 0}, 500, '', function () {
								gallery.animate({
									opacity: 0
								}, {
									queue: false,
									duration: 500,
									complete: function () {
										$('.bg').remove();
										$('#bg').css({position: 'relative'});
										$("html:not(:animated)"+( ! $.browser.opera ? ",body:not(:animated)" : "")).animate({scrollTop: prev_scroll_top}, 500, '', function () {});										
									}
								});

							});

							in_work = 0;
							return false;
						});
					}
				});
			}
		});
	});
	return false;
});
/*--------------------------------------------------*/

});
