// Affiliate Ad loader

if(typeof idevaff == 'undefined') {
	var idevaff = {};											// wrapper object
}

idevaff.server = 'http://www.paravion.ro/';						// paravion server

idevaff.script_id = 'aff_script';								// loader script script ID
idevaff.affURL = 'http://aff.zitec.ro/idevaffiliate.php?id=';	// affiliate tracker URL
idevaff.remoteURL = idevaff.server + 'static/js/affiliate';		// remote scripts folder
idevaff.adID = 'idevaff_ad';									// id of ad div container 
idevaff.iframeID = 'idevaff_iframe';							// container iframe ID
idevaff.formName = 'cautare';									// name of the form inside the iframe
idevaff.iframeLocation = idevaff.server + 'affiliate-form3'; 	// form iframe location / src
idevaff.iframeLoaded = 0;

idevaff.showAd = function () {
	
	this.loader_script = document.getElementById(this.script_id);
	if( this.loader_script ) {
		this.afflink = this.loader_script.getAttribute('afflink');
		if( this.afflink ) {
			//alert(afflink);
			var aff_id_parts = this.afflink.replace(this.affURL,'').split('_');
			this.aff_id = aff_id_parts[0];
			// alert(aff_id);
			this.createAd();
			this.loadAd(this.remoteURL+'/aff'+this.aff_id+'.js');
		}
	}
	
}

idevaff.createAd = function() {
	// creates iframe container DIV, e.g.: <div id="idev_aff" style="width:300px; height: 300px; border:#000 1px solid; padding:2px;"></div>
	this.ad = document.createElement('DIV');
	this.ad.id = this.adID;
	this.ad.style.width = '442px';
	this.ad.style.height = '300px';
	//this.ad.style.border = '#000 1px solid';
	this.ad.style.padding = '1px';
	this.loader_script.parentNode.insertBefore(this.ad, this.loader_script);
}

idevaff.loadAd = function(url) {
    var s = document.createElement('script');
    s.src = url;
    s.type = 'text/javascript';
    // s.owner = this; // closure
    var loaded = false;
    //*
    s.onload = s.onreadystatechange = function() {
    	try{
    		var rs = this.readyState;
    	} catch(e) {
    		alert('rs error');
    		var rs = null;
    	}
	    if (rs && rs!='complete' && rs!='loaded') return;
	    if (loaded) return;
	    loaded = true;
	    //document.body.removeChild(s);
	    //alert('affiliate script loaded');
    	//idevaff.addAffFields();
    }
    //*/
    this.loader_script.parentNode.insertBefore(s, this.loader_script);
}

idevaff.addAffFields = function() {
	
	//alert('add fields called - iframe loaded: ' + idevaff.iframeLoaded);
	
	// populate idev_id and idev_afflink in iframe's location to fill in the search form inside the container iframe at submit time
	var ifr = null;
	try {
		ifr = document.getElementById(idevaff.iframeID);
	} catch(e) {
		//alert('Error - cannot get iframe');
	}
	
	
	if( ifr && (idevaff.iframeLoaded == 1) ) {
	
		//alert('iframe loaded');
		
		var iwin;
		try {
			if(navigator.userAgent.indexOf("Safari") != -1){
				iwin = frames[idevaff.iframeID];
			}else{
				iwin = document.getElementById(idevaff.iframeID).contentWindow;
			}
		} catch(e) {
			//alert('Error on getting iwin');
		}
		//iwin.location = iwin.location + '#' + this.aff_id + '|' + this.afflink;
		
		try {
			iwin.location = idevaff.iframeLocation + '#' + idevaff.aff_id + '|' + idevaff.afflink + '|' + idevaff.server;
			//iwin.location = idevaff.iframeLocation + '#' + idevaff.aff_id ;
		} catch(e) {
			//alert('Location error');
		}
		// alert(iwin.location);	
		
	} else {
		try{
			setTimeout(function(){idevaff.adAffFields();}, 2000);
		} catch(e) {
			//alert('error on recursive call');
		}
	}
	
	/*
	var doc = window.frames[idevaff.iframeID].document;
	*/
}

idevaff.setIframeLoaded = function() {
	//alert('iframe onload handler here - first line');
	if(idevaff.iframeLoaded == 1) {
		//document.getElementById(idevaff.iframeID).onload = function() {};
		return;
	}
	idevaff.iframeLoaded = 1;
	idevaff.addAffFields();
	//alert('iframe onload handler here - last line');
}

var previous_onload = window.onload;

window.onload = function() {
	if( typeof previous_onload == 'function' ) {
		previous_onload();
	}
	idevaff.showAd();
}