
//
// Pixxer Externe orderfuncties
//
// Nic Limper, juni 2006
//
// JSON class door Jason Levitt:
// http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html



// zet een session-cookie om het winkelwagen-id in te bewaren.
// In verband met third-party cookie blocking kan dat niet server-side via de OYPO-site gebeuren... :-(

var winkelwagen=get_cookie('PixxerWinkelwagen')
if (winkelwagen=='') {
	winkelwagen='ea8yt3cp'
	document.cookie="PixxerWinkelwagen="+winkelwagen
}



// De order-functies:

function PixxerOrder(guid) {
	if (objRef('PixxerImg'+guid)) {
		isselected=(objRef('PixxerImg'+guid).src.indexOf('add.gif')>-1?'add':'del')
		ajaxcall('http://www.oypo.nl/pixxer/ajax/api2.0.asp', 'orderbutton', 'cb_orderbutton', guid,isselected,winkelwagen);
		//objRef('PixxerImg'+guid).style.visibility='hidden'
	}
}

function cb_orderbutton(result) {
	jsonObj.removeScriptTag();
	var selectie = result.ajaxResponse[0].foto
	if (selectie) {
		lineCount = selectie.length;
		var string = new Array();
		string.push('<ul class="PixxerThumbs">')
		for (var i = 0; i < lineCount; i++) {
			id=selectie[i]['id']
			string.push('<li id="sel'+id+'"><img src="http://thumbs3.pixxer.nl/thumb.v3.asp?s=30&guid='+id+'"></li>')
			if (objRef('PixxerImg'+id)) objRef('PixxerImg'+id).src='http://www.leusderkrant.nl/pics/krantenf3/oypo/del.gif'
		}
		string.push('</ul>')
		var writestring = string.join('');
		putcontent('PixxerSelection', writestring)
		putcontent('PixxerSelectionCount',lineCount)
		if (objRef('PixxerBestelknop')) {
			if (lineCount>0) {
				styleRef('PixxerBestelknop').display='block'
			} else {
				styleRef('PixxerBestelknop').display='none'
			}
		}
	} else {
		putcontent('PixxerSelection','')
		putcontent('PixxerSelectionCount',0)
		if (objRef('PixxerBestelknop')) styleRef('PixxerBestelknop').display='none'
	}
	
	var reloadinfo = result.ajaxResponse[0].reloadinfo
	if (isObject(reloadinfo)) {
		guid=reloadinfo[0]['id']
		isselected=result.ajaxResponse[0].isselected
		if (isselected) {
			//objRef('PixxerImg'+guid).src='http://www.leusderkrant.nl/pics/krantenf3/oypo/del.gif'
			
		} else {
			objRef('PixxerImg'+guid).src='http://www.leusderkrant.nl/pics/krantenf3/oypo/add.gif'
		}
		//objRef('PixxerImg'+guid).style.visibility='visible'
	}
}


////// diverse nuttige functies en libraries

function objRef(id) {return document.getElementById(id)}
function styleRef(id) {return objRef(id).style }

function putcontent(elementid,content) {
	if (objRef(elementid)) objRef(elementid).innerHTML=content
}

function ajaxcall() {
	var url             = arguments[0];
	var remote_method   = arguments[1];
    var querystring     = '';
    var i               = 0;
	for (i = 3; i < arguments.length; i++) {
		querystring += '&argument[]=' + encodeURIComponent2(arguments[i]);
	}
	querystring += '&response_type=json&callback='+encodeURIComponent2(arguments[2]);
	url = url + '?function=' + remote_method + querystring;
	jsonObj = new JSONscriptRequest(url);
	jsonObj.buildScriptTag();
	jsonObj.addScriptTag();
}

function encodeURIComponent2(tekst) {
	return escape(tekst).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}


// jsr_class.js
//
// JSONscriptRequest -- a simple class for making HTTP requests
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// Sample Usage:
//
// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude + 
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();
//

function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
	try	{
	    this.headLoc.removeChild(this.scriptObj);  
	}
	catch(err) {
		//Handle errors here
	}
}

JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

/////////////////


function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a) {
    return typeof a == 'function';
}


// Get cookie routine by Shelley Powers 

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) { 
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

// Zet winkelwagen status enzo

ajaxcall('http://www.oypo.nl/pixxer/ajax/api2.0.asp', 'orderbutton', 'cb_orderbutton', '-','-',winkelwagen);

function PixxerRefresh() {
	ajaxcall('http://www.oypo.nl/pixxer/ajax/api2.0.asp', 'orderbutton', 'cb_orderbutton', '-','-',winkelwagen);
}
