/**
 * image enlarge
 * @name imagesenlarge.js
 * @author Marghoob Suleman - http://www.marghoobsuleman.com
 * @version 1.1
 * @date January 18, 2009
 * @copyright (c) 2009 Marghoob Suleman (http://www.giftlelo.com)
 */
 
//To do after loading HTML
$(document).ready(function(){
	ImagesEnlarge.setAnimationStatus(setAnimationStatus);
	ImagesEnlarge.init();
});

ImagesEnlarge = {
	settings: {
		interval:0,
		allImageHolder:'featured-products_block_center',
		containerWidth:300,
		containerHeight:300,
		bgColor:'#000000',
		border: true,
		borderColor:'#c3c3c3',
		containerBorderSize:2,
		padding:10,
		divOpacity: 1,
		loaderImage:'modules/imagesenlarge/loader.gif',
		useAnimation:1,//make it zero if you dont want to animate
		imageArray:[],
		hrefHolder:{index:'a.product_image', otherpages:'a.product_img_link', indexBlock:'#featured-products_block_center .ajax_block_product', otherBlock:'#product_list div.center_block'}
	},
	imgHolderDiv:"<div id='bigImageHolder' class='border' style='display:none'></div>",
	init: function() {
		$('body').append(this.imgHolderDiv);
		//set div style
		//$('#bigImageHolder').css({'width' : this.settings.containerWidth+'px', 'height' : this.settings.containerHeight+'px', 'background-color':this.settings.bgColor, 'padding' : this.settings.padding+'px'});
		$('#bigImageHolder').css({'background-color':this.settings.bgColor, 'padding' : this.settings.padding+'px'});
		if(this.settings.border==true) {
			$('#bigImageHolder').css({'border' : this.settings.containerBorderSize+'px solid '+this.settings.borderColor, opacity:this.settings.divOpacity});
		}
		this.alterAttributes();
	},
	getCurrentSourceID: function() {
		var currentpage = $('body').attr('id');
		var pageid;
		var srcBlock;
		if(currentpage=='index') {
			pageid = this.settings.hrefHolder.index;
			srcBlock = this.settings.hrefHolder.indexBlock;
		} else {
			pageid = this.settings.hrefHolder.otherpages;
			srcBlock = this.settings.hrefHolder.otherBlock;
		}
		return {aBlock:pageid, sourceBlock:srcBlock};
	},
	getImagePath: function(path) {
		var extract = path.split("/");
		var sPath = "";
		for(var i=0;i<extract.length-1;i++) {
			sPath+=extract[i]+"/";
		}
		return sPath;
	},
	setID: function(id, path) {
		var sID = id;
		var sPath = path;
		var imgPath = this.getImagePath(sPath);
		var extract = sPath.split("/");
		var imgName = extract[extract.length-1];
		//var largeImg = imgPath+imgName.substr(0, imgName.length-9)+"-large.jpg";

		var reg=new RegExp("home", "g");
		//var largeImg = imgPath.replace(reg,"thickbox")+imgName;
		var largeImg = imgPath.replace(reg,"large")+imgName;
		//alert(imgPath);
		var thickImg = imgPath+imgName.substr(0, imgName.length-9)+"-thickbox.jpg";
		var homeImg = imgPath+imgName.substr(0, imgName.length-9)+"-home.jpg";
		var smallImg = imgPath+imgName.substr(0, imgName.length-9)+"-small.jpg";
		ImagesEnlarge.settings.imageArray[sID] = {small:smallImg, home:homeImg, large:largeImg, thickbox:thickImg};
		//console.debug("ImagesEnlarge.settings.imageArray[sID] " + ImagesEnlarge.settings.imageArray[sID].large);
	},
	getID:function(id) {
		return ImagesEnlarge.settings.imageArray[id];
	},
	alterAttributes: function() {
		//var blocks = $('');
		var sourceBlock = this.getCurrentSourceID();
		var blocks = $(sourceBlock.sourceBlock);
		var total = blocks.length;
		if(total>0) {
			if(ImagesEnlarge.settings.useAnimation==1) {
				$("#bigImageHolder").css({position:'absolute', float:'right', top:'30%', left:'50%'});
			}	
			var sourceA = sourceBlock.aBlock;
			for(var iCount=0;iCount<total;iCount++) {
				var currentBlock = blocks[iCount];
				var id = "product_list_"+iCount;
				currentBlock.id = id;
				var currentA = $("#"+id+ " "+sourceA)[0];
				var aID = "zoomer_"+iCount;
				currentA.id = aID;
				var imgTag = currentA.firstChild;
				var imgName = $(imgTag).attr("src");
				this.setID(aID, imgName);

			//set method
			$("#"+aID).bind('mouseenter',function() {
							//$('#bigImg').attr("src", ImagesEnlarge.settings.imageArray[this.id]);
							
							var imgHTML = "<div class='border' style='position:absolute; background-color:#ffffff' id='imgLoader'><img src='"+ImagesEnlarge.settings.loaderImage+"' border='0' /></div>";
							$("#bigImageHolder").html(imgHTML);
							
							var getImage = ImagesEnlarge.getID(this.id);
							imgHTML += "<div><img id='bigImg' src='"+getImage.large+"' border='0' class='border' /></div>";
							$("#bigImageHolder").html(imgHTML);
							ImagesEnlarge.checkImageLoad();
							var xy = $(this).offset();
							var height = 0;
							var width = $(this).width();
							$("#bigImageHolder").css({'position':'absolute'})
							if(ImagesEnlarge.settings.useAnimation==1) {
								if($('body').attr('id')=='index'){
									$("#bigImageHolder").css( {'left':(xy.left+width+width)+'px', 'top':(xy.top-height)+'px'})
								}
								$("#bigImageHolder").show("fast", function(e) {
																	//	   $("#bigImageHolder").animate( {'left':(xy.left+width)+'px', 'top':(xy.top+height)+'px'}, { queue:false, duration:500 } )
																		   $("#bigImageHolder").animate( {'left':(xy.left+width)+'px', 'top':(xy.top+height)+'px'}, { queue:false, duration:500 } )
																		   });
							} else {
								$("#bigImageHolder").css({'left':(xy.left+width)+'px', 'top':(xy.top+height)+'px'})
								$("#bigImageHolder").show("fast");
							}
						});
			$("#"+aID).bind('mouseleave',function() {
							$("#bigImageHolder").hide("fast");
						});

			}
			
		}
	},
	getImageStatus: function() {
		var img = $("#bigImageHolder #bigImg")[0];
		if(img.complete==true) {
			$('#imgLoader').hide();
			clearInterval(ImagesEnlarge.settings.interval);
		} else {
			//console.debug("2 " + img.complete)
		}
	},
	checkImageLoad: function() {
		ImagesEnlarge.settings.interval = setInterval(ImagesEnlarge.getImageStatus, 500);
		
	},
	setAnimationStatus: function(status) {
		ImagesEnlarge.settings.useAnimation = parseInt(status);
	}
}

