var $ = jQuery.noConflict();
jQuery(document).ready(function() {
	// Menu Hack for ie
	$("#main-menu li:last-child").css("border-right","0");
	$(".corner").corner();
	
	// Share script
	$('.sharethis').mobilyblocks({
		trigger: 'hover',
		direction: 'counter',
		duration:500,
		zIndex:2000,
		widthMultiplier:1.15
	});
	
	// Accordion Script
	$('.sh_accordion .sh_acc_item_title a').click(function(){
		if($(this).parent().next().is(':hidden')){
			$(this).parent().parent().find('.sh_acc_item').slideUp().prev().removeClass('sh_acc_item_title_opened');
			$(this).parent().addClass('sh_acc_item_title_opened');
			$(this).parent().next().slideDown();
		}
	});
	$('.sh_accordion .sh_acc_item:first').show().prev().addClass('sh_acc_item_title_opened');
	
	// Tab Menu Script
	$('.sh_tabs a.sh_tab_title').each(function(){
		$(this).parent().parent().parent().find('.sh_tabs_menu').append($('<li></li>').append(this).click(function(){
			var element = $(this).parent().parent();
			if($(element).find('div [id='+$(this).find('a').attr('rel')+']').is(':hidden')){
				$(element).find('div.sh_tab_item').slideUp();
				$(element).find('div [id='+$(this).find('a').attr('rel')+']').slideDown();
				$(this).parent().find('li a').removeClass('sh_tab_title_active');
				$(this).find('a').addClass('sh_tab_title_active');
			}
		}));
	});
	$('.sh_tabs .sh_tab_item:first').show();
	$('.sh_tabs .sh_tabs_menu a:first').addClass('sh_tab_title_active');
	
	$(".TrueWP_Flickr_Widget a:nth-child(4n+1)").css('marginRight','0');
	$("#footer-wrapper ul li:last-child").css('marginRight','0');
	$("ul.sh_post li:last-child").css('borderBottom','none');
	
	$('#content a > img, #right-col a > img').not("#content div.nivoSlider a img, .sh_lastpost_wrapper a img, .sharethis a img, .flicker a img").each(function(){
		$(this).parent().css('opacity','0');
	});
	
});

// Superfish Menu
jQuery(document).ready(function() {
		$('#main-menu ul.menu').superfish({ 
		animation: {height:'show', opacity:'show'},
		delay:     300,
		speed:     'fast'
		 });
		 
		$('#main-menu ul.menu ul li a').mouseover(function(){
			$(this).find(".title").stop().animate({paddingLeft: '15'});
		});
		$('#main-menu ul.menu ul li a').mouseout(function(){
			$(this).find(".title").stop().animate({paddingLeft: '10'});
		});
		
		$('.sub-pages li a').mouseover(function(){
			$(this).find('span').stop().animate({paddingLeft: '5'});
		});
		$('.sub-pages li a').mouseout(function(){
			$(this).find('span').stop().animate({paddingLeft: '0'});
		});
		$('.sub-pages ul li ul').parent().css('background', 'none');
});

// Toggle Script
jQuery(document).ready(function() {
	$('div.sh_toggle_text').click( function(){
				$(this).parent().find(".sh_toggle_content").slideToggle("slow");
				$(this).toggleClass("sh_toggle_text_opened");
			}
		); 
});

// Custom Button Script
jQuery(document).ready(function() {
	
	$('.custombtnSmall, .custombtnSmallLight').corner("5px");
	$('.custombtnMedium, .custombtnMediumLight').corner("6px");
	$('.custombtnLarge, .custombtnLargeLight').corner("10px");
	$('.custombtnMedium a, .custombtnSmall a, .custombtnLarge a, .custombtnMediumLight a, .custombtnSmallLight a, .custombtnLargeLight a').hover( function(){
			var hoverColor = $(this).attr('rel');
			if($(this).parent().attr('rel')==undefined)
				$(this).parent().attr('rel', $(this).parent().css('backgroundColor'));
			$(this).parent().stop().animate({backgroundColor:$(this).attr('rel')},400);
			if($(this).attr('textColor'))
				$(this).stop().animate({color:$(this).parent().attr('rel')},400);
		},
		function(){
			$(this).parent().stop().animate({backgroundColor:$(this).parent().attr('rel')},400);
			if($(this).attr('textColor'))
				$(this).stop().animate({color:$(this).attr('rel')},400);
		}
	);
});


//window.onload = function(){
$(window).load(function(){
	$('#content a > img, #right-col a > img').not("#content div.nivoSlider a img, .sh_lastpost_wrapper a img, .sharethis a img, .flicker a img").each(function(){
		$(this).parent().css('position','absolute');
		$(this).parent().css('opacity','0');
		$(this).parent().wrap('<div class="loading" style="position:relative"/>');
		
		var w = $(this).outerWidth();
		var h = $(this).outerHeight();
		
		$(this).parent().width(w);
		$(this).parent().height(h);
		if(!$(this).parent().parent().parent().hasClass('sh_image'))
		{
			$(this).parent().parent().width(w+10);
			$(this).parent().parent().height(h+10);
		}
		else
		{
			$(this).parent().parent().width(w);
			$(this).parent().parent().height(h);
		}
		
		$(this).parent().mouseenter(function(){
			$(this).stop().animate({'opacity':'.7'},300,'easeInQuad');
		}).mouseleave(function(){
			$(this).stop().animate({'opacity':'1'}, 300, 'easeInQuad');
		});
	});
	
	var images = $('#content a img, #right-col a > img');
	images.each(function(index, element){
			$(this).parent().delay(index*250).animate({opacity:'1'}, 1000, function(){
				$(this).parent().css('backgroundImage','none');
			  });
	  });
	
	 setImageAni();
	 setFlickrAni();
});

function setImageAni()
{
	$('.sh_lastpost_wrapper, .blog_wrapper').each(function(){
		$(this).find('.innerBg').attr('rel',$(this).outerHeight()-$(this).find('.innerCaption').outerHeight());
		$(this).prepend($(this).find('.innerBg').clone().removeClass('innerBg').addClass('innerBgUp'));
		$(this).find('.innerBg').css('top',$(this).find('.innerBg').attr('rel')+'px').css('opacity','.8');
		$(this).find('.innerBgUp').attr('rel',$(this).outerHeight());
		$(this).find('.innerBgUp').css('bottom',$(this).find('.innerBgUp').attr('rel')+'px').css('opacity','.8');
		$(this).append($(this).find('.innerCaption').clone().removeClass('innerCaption').addClass('innerCaptionUp'));
		$(this).find('.innerCaption').css('top',$(this).find('.innerBg').attr('rel')+'px');
		$(this).find('.innerCaptionUp').css({'bottom':'auto', 'top':'-'+$(this).find('.innerCaptionUp').outerHeight()+'px'});
		$(this).find('.hover_image, .hover_video, .hover_swf').css('top',((($(this).outerHeight()-$(this).find('.hover_image, .hover_video, .hover_swf').outerHeight())/2)-20)+'px').css('left', (($(this).outerWidth()-$(this).find('.hover_image, .hover_video, .hover_swf').outerWidth())/2)+'px');
		$(this).find('.innerReadMore').css('left', parseInt(($(this).outerWidth()-$(this).find('.innerReadMore').outerWidth())/2));
		$(this).find('.innerReadMore').click(function(event){
				event.stopPropagation();
		});
		$(this).mouseenter(function(event){
			event.stopPropagation();
			$(this).parent().find('.innerCaption').stop().animate({top:parseInt($(this).parent().outerHeight())},400, 'easeOutQuad');
			$(this).parent().find('.innerCaptionUp').stop(true).delay(200).animate({top:0},200, 'easeOutQuad');
			$(this).parent().find('.innerBg').stop().animate({top:parseInt($(this).outerHeight()/2)}, 400, 'easeOutQuad');
			$(this).parent().find('.innerBgUp').stop().animate({bottom:($(this).outerHeight()-parseInt($(this).outerHeight()/2))}, 400, 'easeOutQuad');
			$(this).parent().find('.innerReadMore').stop(true).delay(200).animate({bottom:20, opacity:1}, 200, 'easeOutQuad');
			$(this).parent().find('.hover_image, .hover_video, .hover_swf').stop(true).delay(300).animate({opacity:1, top: ($(this).parent().outerHeight()-$(this).parent().find('.hover_image, .hover_video, .hover_swf').outerHeight())/2} ,200, 'easeOutQuad');
		}).mouseleave(function(event){
			event.stopPropagation();
			$(this).parent().find('.innerCaptionUp').stop(true).animate({top:'-'+$(this).parent().find('.innerCaptionUp').outerHeight()},200, 'easeOutQuad');
			$(this).parent().find('.innerCaption').stop().animate({top:parseInt($(this).parent().find('.innerBg').attr('rel'))}, 400, 'easeOutQuad');
			$(this).parent().find('.innerBg').stop().animate({top:parseInt($(this).parent().find('.innerBg').attr('rel')), 'opacity':'.8'}, 400, 'easeOutQuad');
			$(this).parent().find('.innerBgUp').stop().animate({bottom:parseInt($(this).parent().find('.innerBgUp').attr('rel')), 'opacity':'.8'}, 400, 'easeOutQuad');
			$(this).parent().find('.innerReadMore').stop(true).animate({opacity:0, bottom:'-'+$(this).parent().find('.innerReadMore').outerHeight()}, 200, 'easeOutQuad');
			$(this).parent().find('.hover_image, .hover_video, .hover_swf').stop(true).animate({opacity:0, top: ($(this).parent().outerHeight()-$(this).parent().find('.hover_image, .hover_video, .hover_swf').outerHeight())/2-20}, 200, 'easeOutQuad');
		}).click(function(){
			$(this).parent().find('.loading > a').trigger('click');
		});
	});
}
function setFlickrAni()
{
	$('#flickr_badge_wrapper a').mouseenter(function(){
		$(this).stop().animate({'opacity':'.7'},300,'easeInQuad');
	}).mouseleave(function(){
		$(this).stop().animate({'opacity':'1'},300,'easeInQuad');
	});
}

// Randoma string generator
function randomString(size) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<size; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function setActiveSliderItem(sliderID, type)
{
	var curr = $("#slider"+sliderID).data("nivo:vars").currentSlide;
	if(type=="next")
	{
		if(curr<$("#slider"+sliderID).data("nivo:vars").totalSlides-1)
			curr++;
		else
			curr=0;
	}
	$(".slider-wrapper-"+sliderID+" .nivoController li").removeClass("selected");
	$(".slider-wrapper-"+sliderID+" .nivoController a[rel="+curr+"]").parent().addClass("selected");
}
