function hide_preloader() {
	setTimeout(function() {
	jQuery(".body-image-preloader").fadeOut(200);
	jQuery("#body-preloader").fadeOut(1000);
	      // Do something after 5 seconds
	}, 1500);
	
}




var smallMenuDone=false;

function initSmallMenu(){

	if (!smallMenuDone){
		jQuery('#collapsed-menu-elements>li').mouseenter(function(){
			var elem = jQuery(this);

			jQuery('#collapsed-menu-elements>li').not(elem).css('overflow','hidden');
			jQuery(this).css('overflow','visible');
			jQuery('span',this).css('opacity','1')
			jQuery(this).css('width','100%').css('background','#ec564a').css('height','inherit');

		}).mouseleave(function(){
			var elem = jQuery(this);

			jQuery(elem).css('width','40px').css('background','none').height('47px');
			jQuery('#collapsed-menu-elements>li').css('overflow','hidden');

		});


		jQuery('#collapsed-menu-elements').supersubs({
			minWidth:    3,
			maxWidth:    32,
			extraWidth:  1
		}).superfish({delay:0, animationOpen: {opacity:'show'}, animationClose:{opacity:'hide'}, speed: 'slow', autoArrows: false, disableHI:false, onInit:function(){
			jQuery('#collapsed-menu-elements>li').css('width','40px').css('background','none').height('47px');
			jQuery('#collapsed-menu-elements>li').css('overflow','hidden');
			jQuery('#collapsed-menu-elements>li li').css('background','#ec564a');
		}});
	}

	smallMenuDone=true;
}


function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function resizeVideo(){
	if(jQuery('.embedded_videos').length){

		if( jQuery('.embedded_videos').parent().width() < 685){
			var iframe_width = jQuery('.embedded_videos').parent().width();
		}else{
			var iframe_width = 685;
		}
		
  		var_iframe_height = iframe_width/1.37;	
  		jQuery('.embedded_videos iframe ').each(function(){

  			jQuery(this).attr('width',iframe_width);
  			jQuery(this).attr('height',var_iframe_height);
  		});

  		jQuery('.embedded_videos div.video-js ').each(function(){

  			jQuery(this).attr('width',iframe_width);
  			jQuery(this).attr('height',var_iframe_height);
  			jQuery(this).css('width',iframe_width);
  			jQuery(this).css('height',var_iframe_height);
  		});
  		

  	}
}

jQuery(window).load(function(){
	
	jQuery('#collapsed-menu-elements>li').css('width','40px').css('background','none').height('47px');
});


function init_toggle(){
	/*toogle*/
	/*Case when by default the toggle is closed */
	jQuery(".open_title").toggle(function(){ 
			jQuery(this).next('div').slideDown();
			jQuery(this).find('a').removeClass('show');
			jQuery(this).find('a').addClass('toggle_close'); 
			jQuery(this).find('.title_closed').hide();
			jQuery(this).find('.title_open').show();
		}, function () {
		
			jQuery(this).next('div').slideUp();
			jQuery(this).find('a').removeClass('toggle_close');
			jQuery(this).find('a').addClass('show');		 
			jQuery(this).find('.title_open').hide();
			jQuery(this).find('.title_closed').show();
			
	});
	
	/*Case when by default the toggle is oppened */		
	jQuery(".close_title").toggle(function(){ 
			jQuery(this).next('div').slideUp();
			jQuery(this).find('a').removeClass('toggle_close');
			jQuery(this).find('a').addClass('show');		 
			jQuery(this).find('.title_open').hide();
			jQuery(this).find('.title_closed').show();
		}, function () {
			jQuery(this).next('div').slideDown();
			jQuery(this).find('a').removeClass('show');
			jQuery(this).find('a').addClass('toggle_close');
			jQuery(this).find('.title_closed').hide();
			jQuery(this).find('.title_open').show();
			
	});	
	
	
}
jQuery(window).load(function(){

	jQuery('#menuid').supersubs({
		minWidth:    3,
		maxWidth:    32,
		extraWidth:  1
	}).superfish({
		pathClass: 'current-menu-item',
		delay:800, 
		animationOpen: {opacity:'show', height:'show'},
		disableHI:false,
		speed: 'fast', 
		autoArrows: true
	});

	if (jQuery('#small_nav').css('display')!='none'){
		initSmallMenu();
	}
  
  	/*resize FB comments depending on viewport*/

  	setTimeout('viewPort()',3000); 
  	
  	resizeVideo();

  	jQuery( window ).resize( function(){
       viewPort();
       resizeVideo();
       jQuery('div.single-div').height(jQuery(window).height()-60);
    });

  	
	/* Accordion */
	jQuery('.cosmo-acc-container').hide();
	jQuery('.cosmo-acc-trigger:first').addClass('active').next().show();
	
	jQuery(document.body).on('click', '.cosmo-acc-trigger', function() { console.log(123);
	    if( jQuery(this).next().is(':hidden') ) {
			jQuery('.cosmo-acc-trigger').removeClass('active').next().slideUp();
			jQuery(this).toggleClass('active').next().slideDown();
		}
		return false;
	});
	
	
	/* Hide Tooltip */
	jQuery(function() {
		jQuery('a.close').click(function() {
			jQuery(jQuery(this).attr('href')).slideUp();
            jQuery.cookie(cookies_prefix + "_tooltip" , 'closed' , {expires: 365, path: '/'});
            jQuery('.header-delimiter').removeClass('hidden');
			return false;
		});
	});
	
	/* initialize tabs */
	jQuery(function() { 
		jQuery('.cosmo-tabs').tabs({ fxFade: true, fxSpeed: 'fast' });
		jQuery( 'div.cosmo-tabs' ).not( '.submit' ).find( '.tabs-nav li:first-child a' ).click();
	});
	
	/* Hide title from menu items */
	jQuery(function(){
		jQuery("li.menu-item > a").hover(function(){
			jQuery(this).stop().attr('title', '');},
			function(){jQuery(this).stop().attr();
		});
		
		  
	});
	
	
	 /* widget tabber */
    jQuery( 'ul.widget_tabber li a' ).click(function(){
        jQuery(this).parent('li').parent('ul').find('li').removeClass('active');
        jQuery(this).parent('li').parent('ul').parent('div').find( 'div.tab_menu_content.tabs-container').fadeTo( 200 , 0 );
        jQuery(this).parent('li').parent('ul').parent('div').find( 'div.tab_menu_content.tabs-container').hide();
        jQuery( jQuery( this ).attr('href') + '_panel' ).fadeTo( 600 , 1 );
        jQuery( this ).parent('li').addClass('active');
    });
	
		
	/*toogle*/
	init_toggle();
	
  /*Tweets widget*/
   var delay = 4000; //millisecond delay between cycles
   function cycleThru(variable, j){
           var jmax = jQuery(variable + " div").length;
           jQuery(variable + " div:eq(" + j + ")")
                   .css('display', 'block')
                   .animate({opacity: 1}, 600)
                   .animate({opacity: 1}, delay)
                   .animate({opacity: 0}, 800, function(){
                           (j+1 == jmax) ? j=0 : j++;
                           jQuery(this).css('display', 'none').animate({opacity: 0}, 10);
                           cycleThru(variable, j);
                   });
           };
           
   jQuery('.tweets').each(function(index, val) {
     //iterate through array or object
     var parent_tweets = jQuery(val).parent().attr('id');
     var actioner = '#' + parent_tweets + ' .tweets .dynamic .cosmo_twitter .slides_container';
     cycleThru(actioner, 0);
   });
   	
});


function viewPort(){  
	/* Determine screen resolution */
	//var $body = jQuery('body');
	wSizes = [1200, 960, 768, 480, 320, 240];
	wSizesClasses = ['w1200', 'w960', 'w768', 'w480', 'w320', 'w240'];
	
	//$body.removeClass(wSizesClasses.join(' '));
	var size = jQuery(this).width();
	//alert(size);
	for (var i=0; i<wSizes.length; i++) { 
		if (size >= wSizes[i] ) { 
			//$body.addClass(wSizesClasses[i]);

			
			jQuery('.fb_iframe_widget iframe,.fb_iframe_widget span').css({'width':jQuery('#comments').width() });   
			
			break;
		}
	}
	if(typeof(FB) != 'undefined' ){
		FB.Event.subscribe('xfbml.render', function(response) {
			FB.Canvas.setAutoGrow();
		});
	}  
	/** Mobile/Default      -   320px
 * Mobile (landscape)  -   480px
 * Tablet              -   768px
 * Desktop             -   960px
 * Widescreen          -   1200px
 * Widescreen HD       -   1920px*/
	
}

/* ----------------------front page related scripts ----------------------*/

jQuery(document).ready(function(){

	/*show/hide zoom icon when hovering over gallery*/
	jQuery(document.body).on('mouseenter', '.es-carousel li', function() {
	    jQuery(this).find('.zoom-image').show();
	});
	jQuery(document.body).on('mouseleave', '.es-carousel li', function() {
	    jQuery(this).find('.zoom-image').hide();
	});


	var numberOfMenus = jQuery("#nav > ul#menuid > li").size();
	if(numberOfMenus == 0){
		numberOfMenus = 1;
	}
    var widthOfElements = 100/numberOfMenus;
    jQuery(".container-wrapper").css('height', numberOfMenus * 100 + '%');
    jQuery(".container-wrapper > div").css('height', widthOfElements + '%');
    //jQuery("#nav ul li").css('width', widthOfElements + '%');
    jQuery('.nav-dots').fadeOut();
    jQuery('.nav-arrows').fadeOut();

    var rtime = new Date(1, 1, 2000, 12,00,00);
			var timeout = false;
			var delta = 200;
			jQuery(window).resize(function() {
			    rtime = new Date();
			    if (timeout === false) {
			        timeout = true;
			        setTimeout(resizeend, delta);
			    }
			});

			function resizeend() {
			    if (new Date() - rtime < delta) {
			        setTimeout(resizeend, delta);
			    } else {
			        timeout = false;
			        jQuery('.home #main').each(function() {
					    var p = jQuery(this).parent();
					    //jQuery(this).height((p.height() - (jQuery(this).offset().top - p.offset().top))+25);
					});
			    }               
			}

			jQuery('.home #main').each(function() {
			    var p = jQuery(this).parent();
			    //jQuery(this).height((p.height() - (jQuery(this).offset().top - p.offset().top))+25);
			});


			jQuery('.featimg-hover').hover(function(){
				jQuery(this).css('opacity','1');
			}).mouseleave( function(){
				jQuery(this).css('opacity','0');
			});

});     


function initHorizontalMouseWheel(elem, hApi){
	elem.bind( 
        'mousewheel',
        function (event, delta, deltaX, deltaY) 
        { 
            hApi.scrollByX(delta*-50); 
         	return false;
        } 
    ); 
}

jQuery(window).load(function() {
	elastislide_carousel();

	jQuery(".small-device-menu-link").pageslide({ direction: "right", modal: true });

	jQuery('#small-menu-elements li').click(function(){
		jQuery.pageslide.close(function() {
		  goto('#ctn_' + location.hash.replace('#', ''), this);
		});
	});
	jQuery(window).trigger('resize');
	if (jQuery('#collapse-menu-btn i').hasClass("icon-chevron-right")) {
		jQuery('#main').css("width","97%");
		
	}

	jQuery('#collapse-menu-btn').click( function(){
		//jQuery(this).unbind( 'click');
		
		if (jQuery('#collapse-menu-btn i').hasClass("icon-chevron-right")) {
			jQuery.cookie("menu_cookie", null);
			jQuery('#collapse-menu-btn .icon-chevron-right').removeClass('icon-chevron-right').addClass('icon-chevron-left');
			var cookie_value = jQuery('#collapse-menu-btn i').attr('class');
			jQuery.cookie("menu_cookie", cookie_value, { expires: 7 });
			jQuery('#small_nav').fadeOut(300, function(){
				jQuery('.sl-slider h2').animate({ marginLeft: '35%'}, 800, function(){});
				jQuery('.nav-arrows').animate({ left: '35%'}, 800, function(){});

				jQuery('#page-header').removeClass('collapsed').animate({ width: "30%"}, 900, function(){
					jQuery(this).css('overflow','visible');
				});
				jQuery('#main').animate({ width: "70%"}, 700, function() {
					setTimeout(function() {
						jQuery('.branding').fadeIn();			
						jQuery('#nav, .cosmo-social, .search').fadeIn();
						jQuery('#colophon').fadeIn();
						init_freetile();
						jQuery(window).trigger('resize');
						jQuery('#collapse-menu-btn').find('span').fadeIn();
					}, 400);
				});
			});					

		}else if(jQuery('#collapse-menu-btn i').hasClass("icon-chevron-left")){
			jQuery(this).find('span').fadeOut();
			jQuery.cookie("menu_cookie", null);
			jQuery('#collapse-menu-btn .icon-chevron-left').removeClass('icon-chevron-left').addClass('icon-chevron-right');
			var cookie_value = jQuery('#collapse-menu-btn i').attr('class');
			jQuery.cookie("menu_cookie", cookie_value, { expires: 7 });

			jQuery('#page-header .branding').fadeOut(300, function(){
				setTimeout(function() {
					jQuery('.sl-slider h2').animate({ marginLeft: '8%'}, 800, function(){});
					jQuery('.nav-arrows').animate({ left: '8%'}, 800, function(){});

			 		jQuery('#page-header').animate({ width: "40px"}, 700, function() {
						jQuery('#main').animate({ width: "97%"}, 700, function(){
							init_freetile();
							jQuery(window).trigger('resize');
						});
						jQuery(this).addClass('collapsed');
						jQuery(this).animate({ width: "3%"}, 800, function(){
							jQuery(this).css('overflow','visible');
						});
						jQuery('#small_nav').delay(600).fadeIn('slow',function(){
							initSmallMenu();
						});						
					});
				}, 200);
			});
			jQuery('#nav, .cosmo-social, .search').fadeOut(300);
			jQuery('#colophon').fadeOut(300);
		}
		//jQuery(this).bind( 'click');
	});

	jQuery('#collapse-menu-btn').bind('click');

if( typeof comment_submit == 'function' ) { 
        comment_submit();
    }
    
});
    // Slider here

function createSlider(sliderID){
    	var anSliderID = (function() {
			var navigationBullets = '.sliders-navigator ul li.'+sliderID + ' > .nav-dots';
			var $navArrows = jQuery('.sliders-navigator ul li.'+sliderID + ' > .nav-arrows' ),
				$nav = jQuery( navigationBullets + ' > span' ),
				slitslider = jQuery( '#' + sliderID ).slitslider( {
					onBeforeChange : function( slide, pos ) {

						$nav.removeClass( 'nav-dot-current' );
						$nav.eq( pos ).addClass( 'nav-dot-current' );

					}
				} ),

				init = function() {

					initEvents();
					jQuery(window).trigger('resize');
					jQuery('.nav-dots, .nav-arrows').fadeOut();
					jQuery(navigationBullets).fadeIn(200);
					jQuery('.nav-arrows').fadeIn(200);
				},
				initEvents = function() {
					// add navigation events
					$navArrows.children( ':last' ).on( 'click', function() {

						slitslider.next();
						return false;

					} );

					$navArrows.children( ':first' ).on( 'click', function() {
						
						slitslider.previous();
						return false;

					} );
					$nav.each( function( i ) {
					
						jQuery( this ).on( 'click', function( event ) {
							
							var $dot = jQuery( this );
							
							if( !slitslider.isActive() ) {

								$nav.removeClass( 'nav-dot-current' );
								$dot.addClass( 'nav-dot-current' );
							}
							
							slitslider.jump( i + 1 );
							return false;
						
						} );
						
					} );

				};

				return { init : init };
		})();
		anSliderID.init();
}

function moveTabs(elemId){

	// Get number of menu elements and setting widths

	var numberOfMenus = jQuery("#nav > ul#menuid > li").size();
	var widthOfElements = 100/numberOfMenus;
	var currentActiveMenuIndex = jQuery('#nav ul li a.active').parent().index();
	var nameOfId = elemId.replace('#', '#ctn_');
 	var clickedMenuIndex = jQuery('#menu_'+nameOfId.replace('#ctn_', '')).parent().index();
 	var clickedMenuId = jQuery(elemId);
 	jQuery('.nav-dots, .nav-arrows').fadeOut();


 	// Add active class to selected container
 	jQuery('.container-wrapper > div').removeClass('active');
 	jQuery('.container-wrapper').find(clickedMenuId).addClass('active');

 	// Change background depending on the tab
    jQuery('.bgr').find('li').fadeOut(500);
    jQuery('.bgr').find('li[data-id-bg="'+nameOfId+'"]').fadeIn(1000);

    // Verify which way menu has to go
 	var diffCurrentActive = clickedMenuIndex - currentActiveMenuIndex;

 	goto(nameOfId, this);
 	
 	jQuery(nameOfId).animate({scrollTop: 0});

 	setTimeout(function() {
 		elementsForFree(nameOfId);
	}, 1100);

	var diffCount = Math.abs(diffCurrentActive);

	// Check if contentbox is slider
 		
		if(jQuery(nameOfId).hasClass('isSlider')){
      		var sliderID = String(jQuery(nameOfId).attr('data-slider'));
			createSlider(sliderID);
     	}
}

function randsort(c) {
    var o = new Array();
    for (var i = 1; i <= c; i++) {
	    o.push(i);
	}
    return o;
}
function Shuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function elementsForFree(parent){
	//Global vars
	// var randomElements = false;

	if( typeof(elementsForFree_name) !== 'undefined'){
		var randomElements_mode = elementsForFree_name.shuffle_mode;
	}else if( typeof(elementsForFree_thumb_view) === 'undefined'){
		var randomElements = false;
	}
	if (randomElements_mode != 'none') {
		if (randomElements_mode == 'random') {
			var randomElements = true;
		}else if (randomElements_mode == 'ordered'){
			var randomElements = false;
		}

		jQuery('.thumb_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');
		jQuery('.list_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');
		jQuery('.grid_view > div > article').animate({opacity: 0}, 100, 'easeInOutCubic');

		// Check if div contains THUMBS VIEW
		if(jQuery(parent).find("> div.row").hasClass('thumb_view')){

			var numberOfElements = jQuery(parent + ' > .thumb_view > div').size();
			var arr = randsort(numberOfElements);
			if(randomElements === true){
				var r = Shuffle(arr);
			}else{
				var r = arr;
			}
			jQuery(parent).find("> div.row > div > article").each(function(i){
				jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
			})
		}
		// Check if div contains GRID VIEW
		if(jQuery(parent).find("> div.row").hasClass('grid_view')){
			var numberOfElements = jQuery(parent + ' > .grid_view > div').size();
			var arr = randsort(numberOfElements);
			if(randomElements === true){
				var r = Shuffle(arr);
			}else{
				var r = arr;
			}
			jQuery(parent).find("> div.row > div > article").each(function(i){
				jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
			})
		}
		// Check if div contains LIST VIEW
		if(jQuery(parent).find("> div.row").hasClass('list_view')){
			var numberOfElements = jQuery(parent + ' > .list_view > div').size();
			var arr = randsort(numberOfElements);
			if(randomElements === true){
				var r = Shuffle(arr);
			}else{
				var r = arr;
			}
			jQuery(parent).find("> div.row > div > article").each(function(i){
				jQuery(this).delay(r[i]*200).animate({opacity: 1}, 400, 'easeInOutCubic');
			})
		}
	} else{};

}

jQuery(function () {

	// Toggle the single window
	jQuery(".single-clicker").click(function() {
		jQuery('.single-container').animate({'bottom': '0%', opacity: 1}, 1000, 'easeInOutCubic');
		return false;
	});
	jQuery(".single-close").click(function() {
		jQuery('.single-container').animate({'bottom': '-100%', opacity: 0}, 500, 'easeInOutCubic');
		return false;
	});

   

    

     jQuery('#nav ul.home_menu li a').click(function (e) {
     	var menuId = jQuery(this).attr('href');
     	var theNameId = menuId;
     	if(theNameId == null){
     		e.preventDefault();
     		return false;
     	}
		//jQuery(this).addClass('active');
		var currentActiveMenuIndex = jQuery('#nav ul li a.active').parent().index();
		
		// prevent default behaviour
		if(!jQuery(this).hasClass('external_link')) {
	    	e.preventDefault();
		};
	    // cache href attribute
    	if(jQuery.inArray(location.hash.replace('#','') , object_names['menu_objects']) != -1){
		    location.hash = menuId;
		}
     });

});


function goto(id, t){
	//animate to the div id.
    jQuery(".container-wrapper").animate({"top": -(jQuery(id).position().top)}, 1500, 'easeInOutCubic');
}
 
jQuery(window).resize(function() {
        if(this.resizeTO) clearTimeout(this.resizeTO);
        this.resizeTO = setTimeout(function() {
            jQuery(this).trigger('resizeEnd');
        }, 500);
    });
jQuery(window).bind('resizeEnd', function() {
    //do something, window hasn't changed size in 500ms
    goto('#ctn_' + location.hash.replace('#', ''));
});

jQuery(document).ready(function() {
	// Stuff to do as soon as the DOM is ready;
	var activeMenuTab = jQuery('#nav ul li a.active').attr('href');
	//activeMenuTab = activeMenuTab.replace('ctn_', '');
	//goto(activeMenuTab, this); return false;

	jQuery('.item-overlay').live('mouseenter',function(){
		jQuery(this).stop().animate({'opacity':'1'},500);
		jQuery('.featimg-hover .featimg-hover-more',jQuery(this)).stop().animate({'top':'50%'}, 500, 'easeInOutCubic');
	}).live('mouseleave', function(){
		jQuery(this).stop().animate({'opacity':'0'},500);
		jQuery('.featimg-hover .featimg-hover-more',jQuery(this)).stop().animate({'top':'-20%'}, 500, 'easeInOutCubic',
			function(){
				jQuery(this).css('top','120%');
			});
	});

	init_thumbs_hover();
	init_freetile();



  //testimonials
        jQuery('.testimonials-quotes li').hide(); // Hide all divs
        jQuery('.testimonials-quotes li:first-child').show(); // Show the first div
        jQuery('.testimonials-quotes li:first').addClass('active'); // Set the class for active state
        jQuery('.testimonials-authors li a').click(function(){ // When link is clicked
          var currentTab = jQuery(this).attr('href'); // Set currentTab to value of href attribute
          var parenter = '#'+jQuery(this).parent().parent().prev().attr('id');
          var par = '#'+jQuery(this).parent().parent().prev().attr('id') + ' > li';
          
          var div_height = jQuery(parenter).find(currentTab).height();
          jQuery(parenter).find(currentTab).parent().height(div_height);
          jQuery(par).hide(); // Hide all divs
          jQuery(parenter).find(currentTab).fadeIn(); // Show div with id equal to variable currentTab
          jQuery(parenter).find(currentTab).parent().height('auto');
          //Here we change the image
          var data_image = jQuery(this).parent().parent().prev().find(currentTab).attr('data-image');
          jQuery(this).parent().parent().find('.author img').animate({opacity: 0},0).attr('src', data_image).animate({opacity: 0},1).animate({opacity: 1},500);

          //Here we change the testimonial name

          jQuery(this).parent().parent().next().find('li').hide();
          jQuery(this).parent().parent().next().find(currentTab+'-author').fadeIn(1500);
          return false;
    	});	

		/*start prettyPhoto settings*/
		if (prettyPhoto_enb.enb_lightbox) { 
			jQuery(document).ready(function(){
			    /* show images inserted in gallery */
			    jQuery("a[rel^='prettyPhoto']").prettyPhoto({
			            autoplay_slideshow: false,
			            theme: 'light_rounded',
			            deeplinking: false 

			    });

			    /* show images inserted into post in LightBox */
			    jQuery("[class*='wp-image-']").parents('a').prettyPhoto({
			            autoplay_slideshow: false,
			            theme: 'light_rounded',
			            deeplinking: false 

			    });

			    jQuery("a[rel^='keyboardtools']").prettyPhoto({
			            autoplay_slideshow: false,
			            theme: 'light_rounded',
			            social_tools : '',
			            deeplinking: false 

			    });
			});
		};
		/*end prettyPhoto settings*/
	
});

jQuery(window).load(function () {
  	init_freetile();
});	

/* History thing */

jQuery(function(){
	if(location_hash.triger_hash  == '1'){
		if(location.hash != ""){
			var tHash = location.hash;
			if(jQuery.inArray(location.hash.replace('#','') , object_names['menu_objects']) != -1){
			    moveTabs(tHash, null);
			}
			
			var hash = location.hash.replace('#','');
			jQuery('#nav ul li a').removeClass('active');
			jQuery('#small_nav ul li a').removeClass('active');
			jQuery('#menu_' + hash).addClass('active');
			jQuery('#c_menu_' + hash).addClass('active');
			isHash = true;
		}else{
			isHash = false;
			//location.hash = '#ctn_tag_1_portfolio';
			location.hash = location_hash.initial_hash;
		}
	}
	
	if ("onhashchange" in window){
	    jQuery(window).bind('hashchange', function(e){
	        e.preventDefault();
	    	var hash = location.hash;
	    	if(jQuery.inArray(location.hash.replace('#',''), object_names['menu_objects']) != -1){
			    moveTabs(hash, null);
			} else{
				location.hash = location_hash.initial_hash;
			}
	        return false;
	    });
	}
	
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  jQuery(window).hashchange( function(event){
    var hash = location.hash;
    

    // Iterate over all nav links, setting the "selected" class as-appropriate.
    jQuery('#nav ul li a').each(function(){
      var that = jQuery(this);
      that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'active' );
    });
    jQuery('#small_nav ul li a').each(function(){
      var that = jQuery(this);
      that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'active' );
    });

  	event.preventDefault();
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  
});

function init_freetile(){
	jQuery('.masonry').each(function(index) {
	    jQuery(this).freetile({
		    animate: true,
		    elementDelay: 30
		});
	});
}

function init_thumbs_hover(){
	jQuery('.thumb_view .featimg').mouseover(function(){
		jQuery('.entry-title',jQuery(this)).css('opacity','1');
	}).mouseleave(function(){
		jQuery('.entry-title',jQuery(this)).css('opacity','0');
	});
}

/*start tour.js*/
(function($){
    var defaultOptions = {
        "nextClass" : "next",
        "skipClass" : "skip",
        "closeClass" : "close"
    };

    $.fn.tour = function( options ){
        var container = this;
        var index;
        var side;
        var opt = $.extend( {} , defaultOptions , options );
        return this.each(function(){            
            index = 0;
            jQuery( "." + opt.nextClass , this ).click(function(){
                index = parseInt( jQuery( this ).parent().parent().parent().attr('index') );
                side  = jQuery( this ).parent().parent().parent().attr('rel');
                jQuery( container ).each(function( i ){
                    if( index + 1 == parseInt( jQuery( this ).attr('index') ) ){
                        jQuery( this ).fadeTo('slow', 1 );
                        jQuery.cookie(cookies_prefix + '_tour_stap_' + side , index + 1 , {expires: 365, path: '/' } );
                        jQuery( this ).gonext();
                        
                    }else{
                        jQuery( this ).fadeTo('slow', 0 , function(){
                            jQuery( this ).hide();
                        });
                    }
                });
            });

            jQuery( "." + opt.skipClass , this ).click(function(){
                index = parseInt( jQuery( this ).parent().parent().parent().attr('index') );
                side  = jQuery( this ).parent().parent().parent().attr('rel');
                jQuery( this ).parent().parent().parent().fadeTo('slow', 0 , function(){
                    jQuery( this ).hide();
                });
                jQuery.cookie(cookies_prefix + '_tour_stap_' + side , index , {expires: 365, path: '/' } );
            });
            jQuery( "." + opt.closeClass , this ).click(function(){
                side  = jQuery( this ).parent().parent().parent().attr('rel');
                jQuery( this ).parent().parent().parent().fadeTo('slow', 0 , function(){
                    jQuery( this ).hide();
                });
                
                jQuery.cookie(cookies_prefix + '_tour_closed_' + side , 'true' , {expires: 365, path: '/' } );
            });
        });
    }

    $.fn.gonext = function(){
        var h = parseInt((parseInt( jQuery( window ).height() ) - parseInt( jQuery( this ).height()) ) / 2 );
        if( jQuery( this ).offset().top > h ){
            jQuery.scrollTo( jQuery( this ).offset().top - h , 400 );
        }
    }
})(jQuery);
/*end tour.js*/


(function($){
    $.fn.sorted = function(customOptions) {
        var options = {
            reversed: false,
            by: function(a) {
                return a.text();
            }
        };
        $.extend(options, customOptions);
    
        $data = jQuery(this);
        arr = $data.get();
        arr.sort(function(a, b) {
            
            var valA = options.by(jQuery(a));
            var valB = options.by(jQuery(b));
            if (options.reversed) {
                return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;              
            } else {        
                return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;  
            }
        });
        return jQuery(arr);
    };

})(jQuery);

/* ###### Filters ##### */

/* thumbs filter */


jQuery('.filter_container').each(function(){
	var list_class = 'thumbs-list-' + jQuery(this).data('id');
	var controls_class = 'thumbs-splitter-' + jQuery(this).data('id');
	init_thumbs_filter(list_class, controls_class );	
});

function init_thumbs_filter(list_class, controls_class ){
  /*var $list = jQuery('.thumbs-list');
  var $controls = jQuery('ul.thumbs-splitter');*/

  var read_button = function(class_names) {
    var r = {
      selected: false,
      type: 0
    };
    for (var i=0; i < class_names.length; i++) {
      if (class_names[i].indexOf('selected-') == 0) {
        r.selected = true;
      }
      if (class_names[i].indexOf('segment-') == 0) {
        r.segment = class_names[i].split('-')[1];
      }
    };
    return r;
  };
  
  var determine_sort = function($buttons) {
    var $selected = $buttons.parent().filter('[class*="selected-"]');
    return $selected.find('a').attr('data-value');
  };
  
  var determine_kind = function($buttons) {
    var $selected = $buttons.parent().filter('[class*="selected-"]');
    return $selected.find('a').attr('data-value');
  };
  
  var $preferences = {
    duration: 800,
    easing: 'easeInOutQuad',
    adjustHeight: 'dynamic'
  };
  
  var $list = jQuery('.'+list_class);
  var $data = $list.clone();
  
  var $controls = jQuery('ul.'+controls_class);
  
  $controls.each(function(i) {
    
    var $control = jQuery(this);
    var $buttons = $control.find('a');
    
    $buttons.bind('click', function(e) {
      
      var $button = jQuery(this);
      var $button_container = $button.parent();
      var button_properties = read_button($button_container.attr('class').split(' '));      
      var selected = button_properties.selected;
      var button_segment = button_properties.segment;

      if (!selected) {


        var nrOfButtonsMax = '15';
       for(var i = 0; i<=15; i++)
          nrOfButtonsMax += ' ' + 'selected-' + i;
       $buttons.parent().removeClass(nrOfButtonsMax);


        $buttons.parent().removeClass('selected');
        $button_container.addClass('selected-' + button_segment);
        $button_container.addClass('selected');
        
        var sorting_type = determine_sort($controls.eq(1).find('a'));
        var sorting_kind = determine_kind($controls.eq(0).find('a'));
        
        if (sorting_kind == 'all') {
          var $filtered_data = $data.find('div.all-elements');
        } else {
          var $filtered_data = $data.find('div.' + sorting_kind);
        }
        
        if (sorting_type == 'size') {
          var $sorted_data = $filtered_data.sorted({
            by: function(v) {
              return parseFloat(jQuery(v).find('span').text());
            }
          });
        } else {
          var $sorted_data = $filtered_data.sorted({
            by: function(v) {
              return jQuery(v).find('strong').text().toLowerCase();
            }
          });
        }
        
        $list.quicksand($sorted_data, $preferences, function() { 
        // callback function
          //hoverThumbItems();
          //thumbsRepair();
        }
        );
        
      }
      
      e.preventDefault();
    });
    
  }); 
 
}


function elastislide_carousel(){
  
    /*for single page*/
    jQuery('.featimg .single_carousel').elastislide({
      margin : 3 ,
      imageW  : 900,
      minItems : 1
    });

    jQuery('.single_carousel li').each(function(index) {
        jQuery(this).show();
    });

    jQuery(window).trigger('resize'); /*trigger window resize to fix the carousel */

}