
$(document).ready(function(){//

// Testimonial Scripts
$('.testimonial').each(function(){
	var item = $(this);
	item.find('.t_data input').each(function(){
		var data_item = $(this);
		window._data = {};
		 _data = data_item.val().split('&');
		window.data_list = {};
		
		for( el in _data){
			var _the_cont = _data[el].split('=');			
			data_list[_the_cont[0]] = _the_cont[1];
		}
		item.find('.t_text')
			.append('<div class="t_text_wrap_inner"><div>'+data_list.val+'</div></div>');
			
		item.find('.t_name')
			.append('<div>'+data_list.t_name+'</div>');
			
		item.find('.t_location')
			.append('<div>'+data_list.t_location+'</div>');
	});
	var t_text = item.find('.t_text');	
	t_text.width(t_text.find('div.t_text_wrap_inner').size() * 445);	
	item.find('.t_text_wrap').data('moving' , false).scrollLeft(0);
	
	item.find('.t_name div:first').show();
	item.find('.t_location div:first').show();
	t_text.find('div:eq(0)').addClass('active');
		
	});
	$('.t_arr_right').live('click', function(e){
		var item = $(this);
		var prnt = item.parents('.testimonial:eq(0)');
		var text_item = prnt.find('.t_text_wrap');
		
		if(text_item.data('moving') == false){
				text_item.data('moving' , true);
			var active_item = text_item.find('.active');
			if(active_item.is(':last-child')){
				active_item.removeClass('active');
				text_item.find('.t_text div:first').addClass('active');
				text_item.animate({
						scrollLeft : 0
					} , 500 , function(){
						text_item.data('moving' , false);
					});	
			}else{
				text_item.animate({
						scrollLeft : text_item.scrollLeft() + 445
					} , 500 , function(){
						text_item.data('moving' , false);
					});	
				active_item.removeClass('active').next().addClass('active');
			}
			
			location_name_change($(this));
			
		}
	});
	function location_name_change(item){
		var prnt = item.parents('.testimonial:eq(0)');
		var text_item = prnt.find('.t_text_wrap');
		var active_index = text_item.find('.active').index();
			
			prnt.find('.t_name div:visible:first , .t_location div:visible')
				.animate({
					height : 'hide'
				} , 'slow' , function(){
					//console.log('hidden');
					prnt.find('.t_name div:eq('+ active_index +') , .t_location div:eq('+ active_index +')')
						.animate({
							height : 'show'
						}, 'slow', function(){
							//console.log('shown');
						})
				});
	}
	$('.t_arr_left').live('click', function(e){
		var item = $(this);
		var text_item = item.parents('.testimonial:eq(0)').find('.t_text_wrap');
		
		if(text_item.data('moving') == false){
				text_item.data('moving' , true);
			var active_item = text_item.find('.active');
			if(active_item.is(':first-child')){			
				active_item.removeClass('active');
				text_item.find('.t_text .t_text_wrap_inner:last').addClass('active');
				text_item.animate({
						scrollLeft : (text_item.find('.t_text_wrap_inner').size() - 1) * 445
					} , 500 , function(){
						text_item.data('moving' , false);
					});	
			}else{
				text_item.animate({
						scrollLeft : text_item.scrollLeft() - 445
					} , 500 , function(){
						text_item.data('moving' , false);
					});	
				active_item.removeClass('active').prev().addClass('active');
			}
		}
		location_name_change($(this));
		
	});
// Testimonial Scripts END

// Top login animation
	var main_nav = $('.mainnav');
	$('#show_login').click(function(e){
		var item = $(this);
		if(item.hasClass('show_login_button_active')){
			main_nav.animate({
				height : 'hide'
			}, 500 , function(){
				item.removeClass('show_login_button_active');
			});
		}else{
			main_nav.animate({
				height : 'show'
			}, 500 , function(){
				item.addClass('show_login_button_active');
			});
		}
	});
	
// Menu item wrapping
	$('.menu li').each(function(){
		var item = $(this);
		//if(item.)
		var the_size = item.parents('.children').size();
		if( the_size == 0){
			the_size = item.parents('.sub-menu').size();
		}
		if( the_size == 0 ){
			item.find('a:eq(0)').wrap('<span class="menu_item_wrapper"></span>');
		}		
	});

// Sub menu sliding
	var slideTimeout = 400;
	$('.menu .current_page_item .children, .menu .current_page_parent .children, .menu .current_page_item .sub-menu, .menu .current_page_parent .sub-menu').slideDown(slideTimeout);
	var ie_t = setTimeout(function(){
		if(ie7){
			Cufon.replace(".content_l .menu li a, .cufon_it , .before_w h3, .single_post_content .more-link, .navigation .alignleft a, .navigation .alignright a, .search_label label, .content_r_inner h1, .content_r_inner h2, .content_r_inner h3, .content_r_inner h4, .content_r_inner h5, .content_r_inner h6");
		}
	}, slideTimeout+30 );
	$('.page_item').click(function(){
		$('.current_page_item .children, .current_page_parent .children').animate({
			height : 'hide'
		}, slideTimeout);//.slideUp('fast');
	});
	
	wait_cufon();
	$('.sliding_block')
		.append('<div class="slide_btn"></div>')
		.find('.slide_btn').click(function(){
			var item = $(this).parent();
			
			if(item.hasClass('active_slide_btn')){
				item.nextAll('.show_more').animate({
						height : 'hide'
					} , 500 , function(){
						item.removeClass('active_slide_btn')
					});
			}else{				
				item.nextAll('.show_more').animate({
					height : 'show'
				} , 500 , function(){
					item.addClass('active_slide_btn')
				});
			}
		});	

});
function wait_cufon(){
	if($('.web_template .page_item:first a:has(cufon)')){
		
		var t = setTimeout(function(){
			$('.web_cont').css({		
				width : $(window).width()
			});
		}, 100);
	}else{
		var t = setTimeout(function(){
			wait_cufon()
		}, 100);
	}
}
// Sub menu sliding END

// Backgrounds demo
$('.backgrounds_demo a').live('click', function(e){
	var bg_url = $(this).attr('class').slice(5);	
	$('body').removeClass('red_bg blue_bg gradient_bg pastel_bg green_bg brown_bg black_bg white_bg ').addClass(bg_url+'_bg');
	e.preventDefault();
});
$('.skin_demo a').live('click', function(e){
	var skin = $(this).attr('class').slice(5);
	document.cookie = 'userColor=' + skin;
	window.location.reload();
	e.preventDefault();
});
// Backgrounds demo END

 $(function() {
	//the loading image
	var $loader		= $('#st_loading');
	//the ul element
	var $list		= $('.carousel_horizontal');
	
	//the current image being shown
	// a container for background preview. Gallery only
	var $currImage 	= $('#st_main').children('img:first');
	
	
	//calculates the width of the div element
	//where the thumbs are going to be displayed
	buildThumbs();
	
	function buildThumbs(){
		
		var $thumbs_wrapper = $list;
		var $thumbs 		= $thumbs_wrapper.children(':first');
		//each thumb has 180px and we add 3 of margin
		var finalW 			= $thumbs.find('img').length * 300;
		$thumbs.css('width',finalW + 'px');
		
		//alert($thumbs.find('img').length);
		
		//make this element scrollable
		makeScrollable($thumbs_wrapper,$thumbs);
		
	}
	
		
	//clicking on a thumb, replaces the large image
	$list.find('.st_thumbs img').bind('click',function(){
		var $this = $(this);
		$loader.show();
		$('<img class="st_preview"/>').load(function(){
			var $this = $(this);
			var $currImage = $('#st_main').children('img:first');
			$this.insertBefore($currImage);
			$loader.hide();
			$currImage.fadeOut(2000,function(){
				$(this).remove();
			});
		}).attr('src',$this.attr('alt'));
	}).bind('mouseenter',function(){
		$(this).stop().animate({'opacity':'1'});
	}).bind('mouseleave',function(){
		$(this).stop().animate({'opacity':'0.7'});
	});
	
	//function to hide the current opened menu
	function hideThumbs(){
		/*$list.find('li.current')
		.animate({'height':'50px'},400,function(){
			$(this).removeClass('current');
		})
		.find('.st_thumbs_wrapper')
		.hide(200)
		.andSelf()
		.find('.st_link span')
		.addClass('st_arrow_down')
		.removeClass('st_arrow_up');*/
	}

	//makes the thumbs div scrollable
	//on mouse move the div scrolls automatically
	function makeScrollable($outer, $inner){
		var extra 			= 200;
		//Get menu width
		var divWidth = $outer.width();
		//Remove scrollbars
		$outer.css({
			//overflow: 'hidden'
		});
		//Find last image in container
		//var lastElem = $inner.find('img:last');
		var lastElem = $inner.find('.ngg-gallery-thumbnail-box:last');
		$outer.scrollLeft(0);
		//When user move mouse over menu
		$outer.unbind('mousemove').bind('mousemove',function(e){
			// get scrollable cont width by number of elements
			var containerWidth = lastElem[0].offsetLeft + lastElem.outerWidth() + 2*extra;
			var left = (e.pageX - $outer.offset().left) * (containerWidth-divWidth) / divWidth - extra;
			$outer.scrollLeft(left);	
			
		});
	}
	$(window).resize(function(){
		//var left = $('.content_l .menu').offset().left;
		var _width = ($(window).width() < 1080) ? 1080 : $(window).width();
		$('.web_cont').css({
			width : _width
		});
		$('#big_photo_preview').css({				
			width : $(window).width(),
			height : $(window).height()
		});
	});
	
	
});


