//
// HP slider
function hpRotator() {
	$('#bannerPosition-1 ul li').css({opacity: 0.0});
	var $imageArr = $('#bannerPosition-1 ul li'),
	imageArrLength = $imageArr.length;
	$('#bannerPosition-1 ul li:first').css({opacity: 1.0}).addClass('show');  
	rotateSwitch = function(){		
		play = setInterval('rotate()',6000);
	};
	rotateSwitch();
	$("#bannerPosition-1 ul li").hover(function() {
		clearInterval(play);
	}, function() {
		rotateSwitch();
	});	
}
function rotate() {    
	var current = ($('#bannerPosition-1 ul li.show')?  $('#bannerPosition-1 ul li.show') : $('#bannerPosition-1 ul li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#bannerPosition-1 ul li:first') :current.next()) : $('#bannerPosition-1 ul li:first'));    
	next.css({opacity: 0.0,left:"100px"})
	.addClass('show').stop().animate({opacity: 1.0,left:"0px"}, 1000);	
	current.stop().animate({opacity: 0.0,left:"100px"}, 1000).removeClass('show');
};
//

//
// Random Img
function randomImg() {
      $('#radomimg li').css({opacity: 0.0});
      var $imageArr = $('#radomimg li'),
      imageArrLength = $imageArr.length,
      randNum = Math.floor ( Math.random ( ) * imageArrLength + 1 );
      $imageArr.eq(randNum-1).css({opacity: 0.0,display:'block'}).addClass('show').stop().animate({opacity: 1.0}, 1000);
}
//end

//
//

function startOverlay(overlayLink, photoTitle) {
	$("body")
		.append('<div class="overlaymodl"></div><div class="containermodl"></div>')
		.css({"overflow-y":"hidden"});
	$(".overlaymodl").stop().animate({"opacity":"0.6"}, 200, "linear");
	//console.log("Checking for var-- photoTitle, overlayLink ", photoTitle, overlayLink);
	if (overlayLink.match(/.jpg$|.png$|.gif$|.JPG$|.PNG$|.GIF$/)) {
		$(".containermodl").html('<div class="phototitle"><span>'+photoTitle+'</span></div><img src="'+overlayLink+'" alt="" /><div class="photodesc"><a href="'+overlayLink+'">Download</a><a name="close" class="photoclose">X</a></div>');
		$(".containermodl img").load(function() {
			var imgWidth = $(".containermodl img").width();
			var imgHeight = $(".containermodl img").height();
			$(".containermodl")
				.css({
					"top":        "50%",
					"left":        "50%",
					"background":  "#000000",
					"border":     "4px solid #000000",
					"width":      imgWidth,
					"height":     imgHeight,
					"margin-top": -(imgHeight/2),
					"margin-left":-(imgWidth/2) //to position it in the middle
				}).stop().animate({"opacity":"1"}, 200, "linear");
		});
	} else {
		var cl = overlayLink.replace('#','')
		var el = document.getElementById(cl);
		//console.log("Your element: ", el, cl);
		$(".containermodl").html($(el).html()).addClass(cl+'-content');
		$(".containermodl").append('<a name="close" class="photoclose">X</a>');
		var imgWidth = $(".containermodl").width();
		var imgHeight = $(".containermodl").height();
			$(".containermodl")
				.css({
					"top":        "50%",
					"left":        "50%",
					"width":      imgWidth,
					"height":     imgHeight,
					"margin-top": -(imgHeight/2),
					"margin-left":-(imgWidth/2) //to position it in the middle
				}).stop().animate({"opacity":"1"}, 200, "linear");
	}
	window.removeOverlay();
}
function removeOverlay() {
// allow users to be able to close the lightbox
	$(".overlaymodl, .photoclose").click(function(){
		$(".containermodl, .overlaymodl").stop().animate({"opacity":"0"}, 200, "linear", function(){
			$(".containermodl, .overlaymodl").remove();	
		});
	});
}	

//
// Linkify	
String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
};



$(document).ready(function() {

//
//Fancybox	
	if ($(".img-preview").length > 0 ) {
		$(".img-preview").fancybox();		
	}

//
// LightWindow
	$(".lightbox").click(function(event){
		event.preventDefault();
		overlayLink = $(this).attr("href");
		photoTitle = $(this).attr("title");
		window.startOverlay(overlayLink, photoTitle);
	});
	$(".lightbox-inline").click(function(event){
		event.preventDefault();
		overlayLink = $(this).attr("href");
		photoTitle = $(this).attr("title");
		window.startOverlay(overlayLink, photoTitle);
	});
//
// Animate in
	if($('#hp-header').length > 0){
		$('#hp-header').css({opacity: 0.0, top:'-100px'}).stop().animate({opacity: 1.0,top:'0px'}, 1000);
	}
//
// HP Slider
    if ($('#bannerPosition-1').length > 0) {
    	if($('#bannerPosition-1 ul li').length > 1){
    		hpRotator();
    	}
	}
//
// Random Img
    if ($('#radomimg').length > 0) {
    	randomImg();				       
	}
//
// Twitter Feed
	if ($('#twitter').length > 0) {	
		$.getJSON('http://twitter.com/status/user_timeline/shoplaguardia.json?include_rts=true&count=2&callback=?', function(data){
			$.each(data, function(index, item){
				$('#twitter').append('<div class="tweet"><p>' + item.text.linkify() + '</p></div>');
			});
		
		}).success(function() {
			    //alert('Success');
			})
			.error(function() {
				//alert('Twitter may be down :('); 
			});
	}		


});//dom loaded


