// Affiliate Ad loader

if(typeof idevaff == 'undefined') {
	var idevaff = {};											// wrapper object
}
idevaff.server = 'http://aff.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-form23'; 	// form iframe location / src
idevaff.iframeLoaded = 0;
idevaff.iframeWidth = 358;
idevaff.iframeHeight = 250;

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 ) {
			
			var aff_id_parts = this.afflink.replace(this.affURL,'').split('_');
			this.aff_id = aff_id_parts[0];
			
			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 = idevaff.iframeWidth + 'px';
	this.ad.style.height = idevaff.iframeHeight + 'px';
	
	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';
    
    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;
    }
    //*/
    this.loader_script.parentNode.insertBefore(s, this.loader_script);
}

idevaff.addAffFields = function() {
	// 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) ) {
		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');
		}

		try {
			iwin.location = idevaff.iframeLocation + '#' + idevaff.aff_id + '|' + idevaff.afflink + '|' + idevaff.server;
		} catch(e) {
			//alert('Location error');
		}

	} else {
		try{
			setTimeout(function(){idevaff.addAffFields();}, 2000);
		} catch(e) {
			//alert('error on recursive call');
		}
	}
}

idevaff.setIframeLoaded = function() {
	if(idevaff.iframeLoaded == 1) {
		return;
	}
	idevaff.iframeLoaded = 1;
	idevaff.addAffFields();
}

var previous_onload_f4 = window.onload;

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