var kondatorlanguage="sv"
var infoturnedoff = "no";
var showbodytable = "yes";
var loadedfrom = location.host;

if (typeof(SessionIDJobWatcher) == "undefined"){SessionIDJobWatcher = "CA-00-00-00-00-00"}

function productlistinfoqtip(applyto){
if (kondatorlanguage == "sv"){var infotip = "Klicka p\u00E5 markerad artikel f\u00F6r ytterligare produktinformation."}else
{var infotip = "Click on highlighted row to show full product information."}
$(applyto).each(function(){
	$(this).qtip({ content: { text: infotip, title: "Info" },
		position: {
			corner: {
				target: 'topRight',
				tooltip: 'bottomLeft'
			},
			adjust: {
				screen: false, // Keep the tooltip on-screen at all times
				y: 40,
				x: -355
			}
		},
		show: {
			ready: true,
			//when: 'mouseover', 
			solo: false // Only show one tooltip at a time
		},
		hide: { when: 'mouseout', fixed: true },
		style: { 
			width: 185,
			padding: 2,
			'background-color': '#A2D959',
			'font-family': "Arial,Helvetica,Geneva,sans-serif",
			'font-size':'12px',
			title: {
				'font-family':'Arial,Helvetica,Geneva,sans-serif',
				'font-size':'13px',
				padding: 2
			},
			color: 'black',
			textAlign: 'Left',
			border: {
				width: 2,
				radius: 5,
				color: '#A2D959'
			},
			name: 'dark',
			tip: 'bottomLeft' // Inherit the rest of the attributes from the preset dark style
		}
	})
})
}

function throwitemlist() {
/***********************************************************************************
*	(c) Anders Axeen - Kondator AB 2008-02-11
*	2009-02-09 chg KONPubli.nsf to access via public agent to locked db.
*	2009-05-26 chg KONLoad.js at www.kondator.se to just pickup this script.
*	2010-04-07 added fix for SessionIDJobWatcher functionality
*	2010-05-17 added host sensing to route and handle JSONP callback correct
*	2011-10-30 Introduction of Campagne pricing and illustration thereof
***********************************************************************************/
var mycolor = "#9ACd66";
$(document).ready(function(){
	if (document.getElementById("pricelist")){
	var txt = "";
	var strxx = $("#pricelist").html();
	var nStart   = strxx.indexOf("[");
	var nStop    = strxx.indexOf("]");
	var partList = strxx.substring(nStart+1,nStop);
	$("#pricelist").html("")
	var myPath = location.pathname;
	var CallbackHost = location.host
	if (loadedfrom != "www.kondator.se") {CallbackHost = "213.115.41.60"} 
	$.getJSON("/KONPubli.nsf/agtKONJSONP?OpenAgent&PART="+partList+"&CURR=SEK&CALLSERVER=MAIN&format=json&jsoncallback=?", function(json){
		//build the inner html first is cirka
		if (myPath.indexOf("netto")<0 && SessionIDJobWatcher == "CA-00-00-00-00-00"){
			txt="<table width=750px><tr>"
				if (kondatorlanguage=="sv"){
					txt=txt + "<td class=headpris width=16%>Bild</td>";
					txt=txt + "<td class=headpris width=12%>" + "Artikel nr" + "</td>";
					txt=txt + "<td class=headpris width=2%>" + "&nbsp" + "</td>";
					txt=txt + "<td class=headpris width=50%>" + "Beskrivning" + "</td>";
					txt=txt + "<td class=headpris colspan=2 width=20% align=Right>" + "Cirka Pris" + "</td>";
				}else{
					txt=txt + "<td class=headpris width=16%>Images</td>";
					txt=txt + "<td class=headpris width=12%>" + "Part N\u00B0" + "</td>";
					txt=txt + "<td class=headpris width=2%>" + "&nbsp" + "</td>";
					txt=txt + "<td class=headpris width=50%>" + "Description" + "</td>";
					// remove price :txt=txt + "<td class=headpris colspan=2 width=15% align=Right>" + "Price" + "</td>";
					txt=txt + "<td class=headpris colspan=2 width=20% align=Right>" + "&nbsp" + "</td>";
				}
			txt=txt + "</tr>";
			for (i=0;i<json.pricelist.length;i++){
				if (unescape(json.pricelist[i].part) == "&nbsp"){ 
					txt=txt + "<tr class=noclass>";
					txt=txt + "<td class=noclass width=100% colspan=6>" + "&nbsp" + "</td>";
					txt=txt + "</tr>";
				} else if (unescape(json.pricelist[i].part) == "Tillbeh\u00F6r") {
					txt=txt + "<tr class=noclass>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(json.pricelist[i].part) + "</td>";
					}else{
						txt=txt + "<td class=headpris width=100% colspan=6>" + "Accessories" + "</td>";
					}
					txt=txt + "</tr>";
				} else if (unescape(json.pricelist[i].part).substr(0,1) == "/") {
					txt=txt + "<tr class=noclass>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(unescape(json.pricelist[i].part).replace(/\//gi,"")).split(" # ",3)[0] + "</td>";
					}else{
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(unescape(json.pricelist[i].part).replace(/\//gi,"")).split(" # ",3)[1] + "</td>";
					}
					txt=txt + "</tr>";
				} else {
					txt=txt + "<tr class=pricerow>";
					txt=txt + "<td class=pricetext>"+ unescape(json.pricelist[i].PartIconPath) +"</td>";
					txt=txt + "<td class=pricepart>" + unescape(json.pricelist[i].part) + "</td>";
					txt=txt + "<td class=pricetext>" + "&nbsp" + "</td>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=pricetext><b class=\"bold\" style=\"font-weight:800;\">" + unescape(json.pricelist[i].SWCIngress)+ "</b><br>";
						txt=txt + 									 unescape(json.pricelist[i].SWCText)+ "</td>";
						txt=txt + "<td width=20% colspan=2>"
						if (json.pricelist[i].priceOut) {	//Special price exists!
							if (json.pricelist[i].price == json.pricelist[i].priceOut) {		//no special discount on CA
								txt=txt + "<table><tr class='hasoutlet'><td class=pricetext width=15% align=Right><div class=normalprice>" + json.pricelist[i].price + "</div></td>";
								txt=txt + "<td class=pricetext width=5% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
							}else{
								txt=txt + "<table><tr class='hasoutlet'><td class='pricetext outlet' width=15% align=Right><div class=oldprice><div class=normalprice>" + json.pricelist[i].price + "</div></div>";
								txt=txt + "<div class=outletprice>" + json.pricelist[i].priceOut + "</div></td>";
								txt=txt + "<td class=pricetext width=5% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
							}
						}else{	// No special price exists !
							txt=txt + "<table><tr><td class=pricetext width=15% align=Right><div class=normalprice>" + json.pricelist[i].price + "</div></td>";
							txt=txt + "<td class=pricetext width=5% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
						}
						txt=txt+"</td>"
					}else{
						txt=txt + "<td class=pricetext><b class=\"bold\" style=\"font-weight:800;\">" + unescape(json.pricelist[i].USCIngress)+ "</b><br>";
						txt=txt + 									 unescape(json.pricelist[i].USCText)+ "</td>";
						txt=txt + "<td width=20% colspan=2>"
						if (json.pricelist[i].priceOut) {	//Special price exists!
							if (json.pricelist[i].price == json.pricelist[i].priceOut) {		//no special discount on CA
								txt=txt + "<table><tr class='hasoutlet'><td class=pricetext width=15% align=Right><div class=normalprice>" + " " + "</div></td>";
								txt=txt + "<td class=pricetext width=5% align=Right>" + " " + "</td></tr></table>";
							}else{
								txt=txt + "<table><tr class='hasoutlet'><td class='pricetext outlet' width=15% align=Right><div class=oldprice><div class=normalprice>" + " " + "</div></div>";
								txt=txt + "<div class=outletprice>" + " " + "</div></td>";
								txt=txt + "<td class=pricetext width=5% align=Right>" + " " + "</td></tr></table>";
							}
						}else{	// No special price exists !
							txt=txt + "<table><tr><td class=pricetext width=15% align=Right><div class=normalprice>" + " " + "</div></td>";
							txt=txt + "<td class=pricetext width=5% align=Right>" + " " + "</td></tr></table>";
						}
						txt=txt+"</td>"
					}
					txt=txt + "</tr>";
				};
			};
			txt=txt + "<tr>";
			txt=txt + "<td width=19% style='font:10px Verdana'>&nbsp</td>";
			txt=txt + "<td colspan=5 width=81% style='border-top: 1px solid grey'>&nbsp</td></tr>";
			txt=txt + "<tr><td width=19%>&nbsp</td>";
			txt=txt + "<td width=10% align=Left>" + "&nbsp" + "</td>";
			if (kondatorlanguage=="sv"){
				txt=txt + "<td style='font:10px Verdana' colspan=4 width=71% align=Right><i>" + "(OBS! Angivna priser \u00E4r exklusive merv\u00E4rdesskatt)" + "</i></td>"
			}else{
				txt=txt + "<td style='font:10px Verdana' colspan=4 width=71% align=Right><i>" + "(Note! Consult your local retailer for price information)" + "</i></td>"
			}
			txt=txt + "</tr>";
			txt=txt + "</table>";
		}
		else {

			txt="<table width=750px>";
			txt=txt + "<tr>";
			if (kondatorlanguage=="sv"){
				txt=txt + "<td class=headpris width=16%>Bild</td>";
				txt=txt + "<td class=headpris width=12%>" + "Artikel nr" + "</td>";
				txt=txt + "<td class=headpris width=2%>" + "&nbsp" + "</td>";
				txt=txt + "<td class=headpris width=40%>" + "Beskrivning" + "</td>";
				txt=txt + "<td class=headpris width=15% align=Right>" + "Cirka Pris" + "</td>";
				txt=txt + "<td class=headpris width=15% align=Right>" + "Netto Pris" + "</td>";
			}else{
				txt=txt + "<td class=headpris width=16%>Images</td>";
				txt=txt + "<td class=headpris width=12%>" + "Part N\u00B0" + "</td>";
				txt=txt + "<td class=headpris width=2%>" + "&nbsp" + "</td>";
				txt=txt + "<td class=headpris width=40%>" + "Description" + "</td>";
				txt=txt + "<td class=headpris width=15% align=Right>" + "MRS Price" + "</td>";
				txt=txt + "<td class=headpris width=15% align=Right>" + "Net Price" + "</td>";
			}
			txt=txt + "</tr>";
			for (i=0;i<json.pricelist.length;i++){
				if (unescape(json.pricelist[i].part) == "&nbsp"){ 
					txt=txt + "<tr class=noclass>";
					txt=txt + "<td class=noclass width=100% colspan=6>" + "&nbsp" + "</td>";
					txt=txt + "</tr>";
				} else if (unescape(json.pricelist[i].part) == "Tillbeh\u00F6r") {
					txt=txt + "<tr class=noclass>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(json.pricelist[i].part) + "</td>";
					}else{
						txt=txt + "<td class=headpris width=100% colspan=6>" + "Accessories" + "</td>";
					}
					txt=txt + "</tr>";
				} else if (unescape(json.pricelist[i].part).substr(0,1) == "/") {
					txt=txt + "<tr class=noclass>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(unescape(json.pricelist[i].part).replace(/\//gi,"")).split(" # ",3)[0] + "</td>";
					}else{
						txt=txt + "<td class=headpris width=100% colspan=6>" + unescape(unescape(json.pricelist[i].part).replace(/\//gi,"")).split(" # ",3)[1] + "</td>";
					}
					txt=txt + "</tr>";

				} else {
					txt=txt + "<tr class=pricerow>";
					txt=txt + "<td class=pricetext>"+ unescape(json.pricelist[i].PartIconPath) +"</td>";
					txt=txt + "<td class=pricepart>" + unescape(json.pricelist[i].part) + "</td>";
					txt=txt + "<td class=pricetext>" + "&nbsp" + "</td>";
					if (kondatorlanguage=="sv"){
						txt=txt + "<td class=pricetext><b class=\"bold\" style=\"font-weight:800;\">" + unescape(json.pricelist[i].SWCIngress)+ "</b><br>";
						txt=txt + 									 unescape(json.pricelist[i].SWCText)+ "</td>";
					}else{
						txt=txt + "<td class=pricetext><b class=\"bold\" style=\"font-weight:800;\">" + unescape(json.pricelist[i].USCIngress)+ "</b><br>";
						txt=txt + 									 unescape(json.pricelist[i].USCText)+ "</td>";
					}
					txt=txt + "<td>"
					if (json.pricelist[i].priceOut) {	//Special price exists!
						if (json.pricelist[i].price == json.pricelist[i].priceOut) {		//no special discount on CA
							txt=txt + "<table><tr class='hasoutlet'><td class=pricetext width=75% align=Right><div class=normalprice>" + json.pricelist[i].price + "</div></td>";
							txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
						}else{
							txt=txt + "<table><tr class='hasoutlet'><td class='pricetext outlet' width=75% align=Right><div class=oldprice><div class=normalprice>" + json.pricelist[i].price + "</div></div>";
							txt=txt + "<div class=outletprice>" + json.pricelist[i].priceOut + "</div></td>";
							txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
						}
					}else{	// No special price exists !
						txt=txt + "<table><tr><td class=pricetext width=75% align=Right><div class=normalprice>" + json.pricelist[i].price + "</div></td>";
						txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyCA + "</td></tr></table>";
					}
					txt=txt+"</td>"
					txt=txt + "<td>"
					if (json.pricelist[i].priceOut2) {	//Special tp price exists!
						if (json.pricelist[i].price2 == json.pricelist[i].priceOut2) {		//no special discount on tp
							txt=txt + "<table><tr class='hasoutlet'><td class=pricetext width=75% align=Right><div class=normalprice>" + json.pricelist[i].price2 + "</div></td>";
							txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyTP + "</td></tr></table>";
						}else{
							txt=txt + "<table><tr class='hasoutlet'><td class='pricetext outlet' width=75% align=Right><div class=oldprice><div class=normalprice>" + json.pricelist[i].price2 + "</div></div>";
							txt=txt + "<div class=outletprice>" + json.pricelist[i].priceOut2 + "</div></td>";
							txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyTP + "</td></tr></table>";
						}
					}else{	// No special price exists !
						txt=txt + "<table><tr><td class=pricetext width=75% align=Right><div class=normalprice>" + json.pricelist[i].price2 + "</div></td>";
						txt=txt + "<td class=pricetext width=25% align=Right>" + json.pricelist[i].currencyTP + "</td></tr></table>";
					}
					txt=txt+"</td>"

				txt=txt + "</tr>";
				};
			};
			txt=txt + "<tr>";
			txt=txt + "<td width=19% style='font:10px Verdana'>&nbsp</td>";
			txt=txt + "<td colspan=6 width=81% style='border-top: 1px solid grey'>&nbsp</td>";
			txt=txt + "</tr>";
			txt=txt + "<tr>";
			txt=txt + "<td width=19% style='font:10px Verdana;'>&nbsp</td>";
			txt=txt + "<td width=10%>" + "&nbsp" + "</td>";
			if (kondatorlanguage=="sv"){
				txt=txt + "<td style='font:10px Verdana' colspan=4 align=Right><i>" + "(OBS! Angivna priser \u00E4r exklusive merv\u00E4rdesskatt)" + "</i></td>"
			}else{
				txt=txt + "<td style='font:10px Verdana' colspan=4 align=Right><i>" + "(Note! Prices indicated above exclude VAT)" + "</i></td>"
			}
			txt=txt + "</tr>";
			txt=txt + "</table>";

		};

		$("#pricelist").html(txt);
		$(".pricerow").css("cursor","pointer")
		$(".pricerow").hover(function() {
			$(this).addClass("priceback");
			}, function() {
			$(this).removeClass("priceback");
		});

		productlistinfoqtip('table:has(td.headpris):first')

		var hasoutletimage = "<img src='/kondator/kon_www.nsf/hasoutletjustnu_se.png?OpenImageResource'>"
		if (kondatorlanguage=="en"){hasoutletimage = "<img src='/kondator/kon_www.nsf/hasoutletjustnu_en.png?OpenImageResource'>"}
		
		$('tr:has(tr.hasoutlet)').each(function(){
		   $(this).qtip({
				   content: { text : hasoutletimage },
				   position: {
					   	corner: { target: 'topRight', tooltip: 'bottomLeft'	},
			         	adjust: { x: -575, y: 15 }
				   },
				   style: { background: 'transparent', border: { color: 'transparent', width: 0 }, tip: { color: 'transparent' } },
				   show: { when: 'mouseover', solo: false },
				   hide: { when: 'mouseout', fixed: true }
			   });
		})
		$('td:has(tr.hasoutlet)').css({'background-image': 'url(/kondator/kon_www.nsf/tn_hasoutletjustnu_en.png?OpenImageResource)',
		   'background-repeat': 'no-repeat',
		   'background-position':'right top'})

		
		/*var outletimage = "<img src='/kondator/kon_www.nsf/outletjustnu_se.png?OpenImageResource'>"
		if (kondatorlanguage=="en"){outletimage = "<img src='/kondator/kon_www.nsf/outletjustnu_en.png?OpenImageResource'>"}

		$('tr:has(td.outlet):first').each(function(){
		   $(this).qtip({
			   content: { text : outletimage },
			   position: {
				   	corner: { target: 'topRight', tooltip: 'bottomLeft'	},
		         	adjust: { x: -75, y: 15 }
			   },
			   style: { background: 'transparent', border: { color: 'transparent', width: 0 }, tip: { color: 'transparent' } },
			   show: { when: 'mouseover', solo: false },
			   hide: 'mouseout'
		   });
		});
*/

		//Quick tooltip just to show the image if moused over
		$('img.prodicon').each(function()
		{
		   $(this).qtip(
		   {
		      content: {
		         text: $(this).attr('previewimage'), // Use the rel attribute of each element for the url to load
		         title: {
    	           text: $(this).attr('tiptitle')
		         }
		      },
		      position: {
		         corner: {
		            target: 'bottomMiddle', // Position the tooltip above the link
		            tooltip: 'topMiddle'
		         },
		         adjust: {
		            screen: true // Keep the tooltip on-screen at all times
		         }
		      },
		      show: { 
		         when: 'mouseover', 
		         solo: false // Only show one tooltip at a time
		      },
		      hide: 'mouseout',
		      style: {
		        tip: true, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
				title: { 'font-family':'Arial,Helvetica,Geneva,sans-serif',
						 'font-size':'12px'
				}, 
		         border: {
		            width: 0,
		            radius: 4
		         },
		         name: 'light', // Use the default light style
		         width: 'auto' // was 200 Set the tooltip width
		      }
		   })
		});
		// Just bluntly open the product card when the row is clicked ...
		$('tr:has(td.pricetext)').click(function () {
			if (loadedfrom == "www.kondator.se") {
				//CallbackHost = "213.115.41.60" 
//				window.location.href = "http://" + loadedfrom + "/kondator/kon_prod.nsf/productdata/"+$(this).find('td.pricepart').text()+"?OpenDocument&topimagedisplay=HideTop"
				window.location.href = "/kondator/kon_prod.nsf/productdata/"+$(this).find('td.pricepart').text()+"?OpenDocument&topimagedisplay=HideTop"
			}else{
				CallbackHost = "213.115.41.60"
				window.location.href = "/kondator/kon_prod.nsf/productdata/"+$(this).find('td.pricepart').text()+"?OpenDocument&topimagedisplay=HideTop"
//				window.location.href = "http://" + CallbackHost + "/kondator/kon_prod.nsf/productdata/"+$(this).find('td.pricepart').text()+"?OpenDocument&topimagedisplay=HideTop"
			};
		});
		});
		document.getElementById("pricelist").style.display="inline";
	};
});

};

function check_language_prefered(){
	if (getCookie("kondatorlanguage") == "")
		{
		if (navigator.browserLanguage){language=navigator.browserLanguage}
		if (navigator.userLanguage){language=navigator.userLanguage}
		if (navigator.systemLanguage){language=navigator.systemLanguage}
		if (navigator.language){language=navigator.language}
		if (language.indexOf('-')==2) {language=language.substring(0,2);}
		if (language=='sv')	// { document.write("Hello there!"); }
			{
			return "sv";
			}
			else
			{
			return "en";
			}
		}
		else
		{
		return getCookie("kondatorlanguage");
		}
	return "sv";
}

function setCookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	/* if the expires variable is set, make the correct expires time, the current script below will set
	it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24 */
	if ( expires ){expires = expires * 1000 * 60 * 60 * 24}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
//	( ( path ) ? ";path=" + path : "" ) +
	( !( path ) ? ";path=" + "/" : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
};

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 "";
};


$(document).ready(function(){
	kondatorlanguage = check_language_prefered();
	throwitemlist();
})
