$(document).ready(function(){
	// current script dir
	var dir =  getScriptLoc();	
		
	// Supported file extensions [fancybox]
	var thumbnails = 'a:has(img)[href$=".bmp"],a:has(img)[href$=".gif"],a:has(img)[href$=".jpg"],a:has(img)[href$=".jpeg"],a:has(img)[href$=".png"],a:has(img)[href$=".BMP"],a:has(img)[href$=".GIF"],a:has(img)[href$=".JPG"],a:has(img)[href$=".JPEG"],a:has(img)[href$=".PNG"]';	
	// bind class and rel
	$(thumbnails).addClass("fancybox").attr("rel","fancybox");				

	// start the loop
	startBind();
	
	// loop the 'reBind' function
	function startBind() {
		inID = setInterval(reBind, 500);
	}
	
	// re-binding elemnts
	c = 1;
	function reBind() {
		// only start the loop if #loading found (or c==50 to prevent infinite loop).
		// this way we ensure that the compo if fully loaded to DOM.
		if ($('#loading').size() > 0 || c == 50) {
		
		//replace img src
		var img = $("#imgCal1, #imgCal2");				
		img.attr('src', '/wp-content/themes/almog_heb/img/calendar.png');
		img.show();
	
	// load fancybox		
	$.getScript(dir + 'jquery.fancybox.js', function(){
		// fancybox settings
		$('a.fancybox').fancybox({
			'imageScale': true,
			'padding': 10,
			'zoomOpacity': true,
			'zoomSpeedIn': 500,
			'zoomSpeedOut': 500,
			'zoomSpeedChange': 300,
			'overlayShow': true,
			'overlayColor': "#666666",
			'overlayOpacity': 0.3,
			'enableEscapeButton': true,
			'showCloseButton': true,
			'hideOnOverlayClick': true,
			'hideOnContentClick': false,
			'frameWidth':  560,
			'frameHeight':  340,
			'centerOnScroll': true
		});
	});
	
	// load cycle script		
	$.getScript(dir + 'jquery.cycle.all.min.js', function(){
		
		// cycle settings
		$('.flash').cycle({ 
			timeout: 5000,
			speed: 2000,
			height:270
		});
		
		$('#deals').cycle({ 
			timeout: 5000,
			speed: 2000,
			height:135,
			fx: 'scrollUp'
		});
	});		
	
			// stop loop if #loading is in the DOM					
			clearInterval(inID);
		}
		c++;
	}
			
	// get current script dir
	function getScriptLoc() {
		scripts = document.getElementsByTagName('script');
		for (g=0; g <= scripts.length; g++) {
			if (scripts[g].src.indexOf('int.js') != -1) {
				return  scripts[g].src.replace('int.js', '');
			}
		}
	}
	
	// add titles to imgs for fancybox	
	$.fn.getTitle = function() {
		var arr = $("a.fancybox");
		$.each(arr, function() {
			var title = $(this).children("img").attr("title");
			$(this).attr('title',title);
		})
	}
		
});

