var Jq = jQuery.noConflict();

compare_cookie_name = "hide_compare_cname";
cookie_val = "1";
cookie_expiry_days = 10;

function generate_comparison_container_html(details){
	detail_url = details.detail_url
	itemsList = details.list;
	html = "<ul>";
	maxCount = 2;
	for (i = 0; i < itemsList.length; i++) {
		if(i<maxCount){
			tmpItem = itemsList[i];
			html = html + "<li><div class='left-category'>";
			html = html + "<a href='" + tmpItem.view_url +"'>";
			html = html + "<img src='"+ tmpItem.image_url +"' alt='"+ tmpItem.name +"' />";										
			html = html + "</a></div><div class='right-category'><h4>";
			html = html + tmpItem.name;
			html = html + "</h4><ul><li><div onclick=ajaxified_remove_from_compare("+"'"+tmpItem.remove_url+"'"+") class='prod-com'>remove</div><span>|</span></li>"; 
			html = html +"<li><a href='" + tmpItem.view_url +"'>view product</a></li></ul></div>";
			html = html + "</li>";
		}
	}	
	html = html + "</ul>";
	return html;
}

function generate_pop_up_html(details){
	detail_url = details.detail_url;
	itemsList = details.list;
		html = "<div class='compare-content'>";
		html = html
				+ "<div class='left-block'><div class='title'>Products Added</div><div class='sub-title'></div>";
		tmpItem = itemsList[0];
		for (i = 0; i < itemsList.length; i++) {
			tmpItem = itemsList[i];
			html = html
					+ "<div class='product-block'>";
			html = html + "<a href='" + tmpItem.view_url
					+ "'><img alt='" + tmpItem.name
					+ "' src='" + tmpItem.image_url
					+ "' /></a>";
			html = html + "<div class='details-box'>";
			html = html + "<a href='" + tmpItem.view_url
					+ "' class='product-name'>" + tmpItem.name
					+ "</a>";
			html = html + "<div class='controls'>";
			html = html + "<a href='#' onclick=ajaxified_remove_from_compare("+"'"+tmpItem.remove_url+"'"+") style='color:#666666;text-decoration:none;' >remove</a>";
			html = html + "&nbsp;|&nbsp;<a href='" + tmpItem.view_url
					+ "' style='color:#666666;text-decoration:none;'>view product</a>";
			html = html + "</div></div></div>";
		}
		html = html
		+ "<a href='#' onclick=\"popWin('"+detail_url+"','compare','top:0,left:0,width=850,height=600,resizable=yes,scrollbars=yes');location.reload(true);\" style='color:#666666;text-decoration:none;'>View Comparison Chart</a>";
		html = html
				+ "</div><div class='right-block'><p>Your item has been added. Click link below to view comparison chart or return to browsing.</p><p>Compare up to 4 products at a time</p>";
		html = html
				+ "<p><a href='#' onclick=\"popWin('"+detail_url+"','compare','top:0,left:0,width=850,height=600,resizable=yes,scrollbars=yes');location.reload(true);\">View Comparison Chart</a> | <a href='Javascript:location.reload(true);'>Return to Browsing</a></p>";
		html = html
				+ "<!-- <input id = 'popup-checkbox' class='dont-show-again' type='checkbox' name='show'/>";
		html = html
				+ "<label>  Don't show this message again</label> -->";
		html = html + "</div></div>";
		return html;
}

function extract_json_from_response(response){
	comparejsonIdx = response.indexOf('compare_json');		
	response = response.substring(comparejsonIdx);
	start = response.indexOf('>');
	end = response.indexOf('</div>');
	response = response.substring(start+1, end);
	details = response;
	details = jQuery.parseJSON(details);
	return details; 
}
function cufonify_popup(){
	Cufon.replace('.compare-content .left-block .sub-title', {fontFamily: 'ITC Avant Garde Gothic Std Book'});
	Cufon.replace('.compare-content .left-block .title', {fontFamily: 'ITC Avant Garde Gothic Std Demi'});	
	Cufon.replace('.compare-content .left-block .product-block .product-name', {fontFamily: 'ITC Avant Garde Gothic Std Demi'});	
	Cufon.replace(' .compare-content .left-block .view-chart', {fontFamily: 'ITC Avant Garde Gothic Std Medium'});	
}


function ajaxified_add_to_compare(url) {
//	eraseCookie(compare_cookie_name);
	Jq.get(url, function(response) {
		details = extract_json_from_response(response);
		pop_up_html = generate_pop_up_html(details);
		comparison_container_html = generate_comparison_container_html(details);
		Jq("#popup_container").html(pop_up_html);
		Jq("#comparison_container").html(comparison_container_html);
		
		cufonify_popup();
		//Jq( 'a.ui-dialog-titlebar-close' ).remove();
		Jq("#dialog").dialog({
			  close: function(event, ui) { location.reload(true);}
			});		
		if (readCookie(compare_cookie_name) == null) {
			Jq("#dialog").dialog("open");
			Jq("#dialog").dialogButtons('Close', 'enabled').css('color','white');
		}
		else{
			location.reload(true);
			}
		add_checkbox_event_handler();
	});
	return false;
}

function ajaxified_remove_from_compare(url) {
	Jq.get(url, function(response) {
		details = extract_json_from_response(response);
		pop_up_html = generate_pop_up_html(details);
		comparison_container_html = generate_comparison_container_html(details);
		Jq("#popup_container").html(pop_up_html);
		Jq("#comparison_container").html(comparison_container_html);
		cufonify_popup();
		if (readCookie(compare_cookie_name) == null) {
			Jq("#dialog").dialog("open");
		}
		add_checkbox_event_handler();
		});
	return false;
}

function add_checkbox_event_handler(){
	Jq('.dont-show-again').click(function() {
		if(Jq('.dont-show-again:checked').length == 1){
			createCookie(compare_cookie_name, cookie_val, cookie_expiry_days);				
		}else{
			eraseCookie(compare_cookie_name);									
		}
	});
}

Jq(document).ready(function() {
	Jq("#dialog").dialog( {
		autoOpen : false,
		width : 625
	});
});

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var cookie_str = document.cookie;
	if(cookie_str.indexOf(name)<0){
		return null;
	}
	var ca = cookie_str.split(';');
	for ( var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name, "", -1);
}

