$(document).ready(function() {
	if(document.getElementById('total_favorites')) {
		if(jQuery('#total_favorites').attr('class') == 'hide') {
			var favorite_cookie_id = getCookie('favorite');
			if(favorite_cookie_id == '') {
				favorite_cookie_id = jQuery.uuid();			
			}
			setCookie('favorite', favorite_cookie_id, 365);
			jQuery.ajax({
				type: "POST",
				url: "/favorite/total",
				data: "favorite_cookie_id="+favorite_cookie_id,
				dataType: "json",
				success: function(result){			
					if(result.total) {
						if(document.getElementById('total_favorites')) {
							jQuery('#total_favorites').html('You have '+result.total + ((result.total==1)?' entry':' entries') + ' in your Enquiry List');
							jQuery('#total_favorites').attr('href', '/favorite/view/cookie_id/'+favorite_cookie_id);
							jQuery('#total_favorites').attr('class', '');
						}
					}
				}

			});
		}
	}
});


function closeModalWindow() {
	$.unblockUI();
	return false;
}

function openModalWindow(elementId, css) {
	$.blockUI({ 
		message: $('#'+elementId),
		css: css
	});
}


function showImage(image_url, image_width, image_height){
	image_width = image_width == "" ? 400 : image_width;
	image_height = image_height == "" ? 380 : image_height;

	var w = screen.width;
	var h = screen.height;
	var ww = image_width * 1 + 30;
	var wh = image_height * 1 + 45;
	var wx = (w - ww)/2;
	var wy = (h - wh)/2;
	
	
	var imageWin = window.open(
		'',
		"ProductImageWindow",
		"titlebar=yes, toolbar=no, menubar=yes, status=no, directories=no, resizable=yes, scrollbars=yes, top=" + wy.toString() + ", left=" + wx.toString() + ", width=" + ww.toString() + ", height=" + wh.toString() + ""
	);
	imageWin.focus();
	
	imageWin.document.body.innerHTML = "";
	imageWin.document.write('<body style="padding:5px;margin:0px">');
	imageWin.document.write('<div align="center"><img hspace="0" vspace="0" src="' + image_url + '"></div><br/>');
	imageWin.document.write('<div align="center" style="font-family:arial;font-color:black;font-size:11px;"><a href="javascript:window.close();" style="color:#0000AA;">Close Window</a></div>');
	imageWin.document.write('</body>');
	
	
}









function rotate(action, total) {	
		
	
	var next_item_index = 0;
	
	if(action == 'pre') {
		next_item_index = ( ( document.getElementById('rotator').index - 1) < 0 ) ? (total - 1) : ( document.getElementById('rotator').index - 1);
	} else if(action == 'next') {
		next_item_index = ( ( document.getElementById('rotator').index + 1) >= total ) ? 0 : ( document.getElementById('rotator').index + 1);
	}

	document.getElementById('rotator').index = next_item_index;

	jQuery('#rotator').children('li').attr('class', '');
	/*
	jQuery.each(jQuery('#rotator li'), function() {		
		this.className = '';		
    });*/

	
	jQuery('#rotator li').eq(document.getElementById('rotator').index).attr('class', 'current');
	
}


function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/";	
}

function getCookie(c_name)
{	
	if (document.cookie.length>0) {		
		c_start=document.cookie.indexOf(c_name + "=");		
		if (c_start!=-1) {			
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;						
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}	
	return "";
}



function addToFavorite() {

	var css = {
		position:		'fixed',
		width:          '180px', 		
		margin:			'0 0 0 -100px',
		top:            '190px', 
		left:           '50%', 
		textAlign:		'left',
		cursor:         null			
	};
		
	openModalWindow('add_to_favorite', css);

	var favorite_cookie_id = getCookie('favorite');
	if(favorite_cookie_id =='') {		
		favorite_cookie_id = jQuery.uuid();		
	}	
	setCookie('favorite', favorite_cookie_id, 365);

	jQuery('#favorite_cookie_id').attr('value', favorite_cookie_id);	

	return false;
}

function checkAllFavorites() {
	jQuery('#favorite_table input:checkbox').attr('checked', 'checked');
	return false;

}

function uncheckAllFavorites() {	
	jQuery('#favorite_table input:checkbox').removeAttr('checked');
	return false;
}

function sendFavoriteEnquiry() {
	jQuery('#favorite_form').attr('action', '/favorite/send');	
}

function deleteFromFavorites() {	
	jQuery('#favorite_form').attr('action', '/favorite/delete');	
}

function init_cookie() {
	var favorite_cookie_id = getCookie('favorite');
	if(favorite_cookie_id == '') {
		favorite_cookie_id = jQuery.uuid();			
	}
	setCookie('favorite', favorite_cookie_id, 365);
	
	jQuery.ajax({
		type: "POST",
		url: "/favorite/total",
		data: "favorite_cookie_id="+favorite_cookie_id,
		dataType: "json",
		success: function(result){			
			if(result.total) {
				if(document.getElementById('total_favorites')) {
					jQuery('#total_favorites').html('You have '+result.total + ((result.total==1)?' entry':' entries') + ' in your Enquiry List');
					jQuery('#total_favorites').attr('href', '/favorite/view/cookie_id/'+favorite_cookie_id);
					jQuery('#total_favorites').attr('class', '');
				}
			}
		}

	});

}

function saveCookieInSession() {
	var favorite_cookie_id = getCookie('favorite');
	if(favorite_cookie_id == '') {
		favorite_cookie_id = jQuery.uuid();			
	}
	setCookie('favorite', favorite_cookie_id, 365);
	$.ajax({
		type: "POST",
		url: "/cookie/add-to-session",
		data: "name[]=favorite_cookie_id&value[]="+favorite_cookie_id
	});

}


