// setup events
var ajURL = '/index.php?c=site_profile&ajax=1';
var imageURL = '/static/newdiz/images/';

var adTypes = new Object (
	
);

/*******************************************************************************
 *	onLoad Events
 *******************************************************************************/

function loadSiteProfileEvents() {
	$("#input-search input").click(function() {typeSwitch(this);});
	$("#input-search span[class!='ad_type_disabled']").click(function() {typeSwitch(this); return false;});
	
	var ad_type = $("#input-search input[name='type_item']:checked").val();	
	var zone_id = $("#input-search-items input[name='" + ad_type + "-radio']:checked").val();
	retrieveZoneInfo(zone_id);
	
	//w = document.getElementById('chart-line-4');
	//alert(w.clientWidth);
	
	$("#screenshots a[id='screenshot']").each(function() {
		$(this).click(function () {popupPic($(this).attr('href')); return false;});
	});
		
	$('#zone-search input[type=radio]').each(function(i){
		if (i == 0) {changeAdView($(this).attr('id'), $(this).attr('value'));}
	});
	
}

function popupPic(sPicURL) { 
	window.open("/popup.html?"+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200");
}
	
/*******************************************************************************
 *	Actions
 *******************************************************************************/

function changeSelectedFontWeight(id) {
    if (!id) {id = 0;}
    
    var zoneSelector;
    var div = document.getElementById(id + '-zone-selector');
    if (div) {
	var typeId = div.parentNode.id;
	$('#zone-search div[id=' + typeId + '] div[id*=-zone-selector]').each(function(i){
	    if (this.id == id + '-zone-selector') {
		this.className = 'textb';
	    } else {
		this.className = 'text';
	    }
	});
    }
}

function changeAdView(inType, id) {
	var image = '';
	var alt = '';
	if (!id) {id = 0;}
	
	changeSelectedFontWeight(id);
	
	if (inType == 't728x90') {
		image = '728x90.jpg';
	} else if (inType == 't468x60') {
		image = '468x60.jpg';
	} else if (inType == 't725x260') {
		image = 't725x260.gif';
	} else if (inType == 't120x600') {
		image = '120x600.jpg';
	} else if (inType == 't160x600') {
		image = '160x600.jpg';
	} else if (inType == 't300x250') {
		image = 't300x250.jpg';
	} else if (inType == 't372x284') {
		image = 't372x284.jpg';
	} else if (inType == 't727x55') {
		image = 't727x55.jpg';
	} else if (inType == 'tXxY') {
		image = '';
	} else if (inType == 'b725x260') {
		image = '725x260.jpg';
	} else if (inType == 'b728x90') {
		image = '728x90.jpg';
	} else if (inType == 'b468x60') {
		image = '468x60.jpg';
	} else if (inType == 'b120x600') {
		image = '120x600.jpg';
	} else if (inType == 'b160x600') {
		image = '160x600.jpg';
	} else if (inType == 'b300x250') {
		image = '300x250.jpg';
	} else if (inType == 'b372x284') {
		image = '372x284.jpg';
	} else if (inType == 'b727x55') {
		image = '727x55.jpg';
		
	// also the popunders
	} else if (inType == 'p725x260') {
		image = 'p763x320.jpg';
	} else if (inType == 'pXxY') {
		if (id == '4') {
			image = 'p839x661.jpg';
		} else {
			image = 'p839x661.mu.jpg';
		}
		alt = 'XxY';
	}
	
	// now the new types
	else if (inType == 'ivt300x250') {
		// image = 'ivt640x393.jpg';
		// image = 'ivt495x430.jpg';
		// image = 'ivt640x361.jpg';
		image = 'ivt835x469.jpg';
		if (id == 11) {image = 'ivt_mp835x469.jpg';}
	} else if (inType == 'ivi300x250') {
		// image = 'ivi640x393.jpg';
		// image = 'ivi495x430.jpg';
		image = 'ivi835x469.jpg';
		if (id == 11) {image = 'ivi_mp835x469.jpg';}
	} else if (inType == 'bb993x270') {
		image = 'bb900x245.jpg';
	}
	
	var imageWidth = '';
	var imageHeight = '';
	if (image.length != '') {
		var tmp = image.split("x");
		imageWidth = tmp[0];
		if (imageWidth.substring(0, 6) == 'ivt_mp' || imageWidth.substring(0, 6) == 'ivi_mp') {imageWidth = imageWidth.substring(6, imageWidth.length);}
		else if (imageWidth.substring(0, 3) == 'ivt' || imageWidth.substring(0, 3) == 'ivi') {imageWidth = imageWidth.substring(3, imageWidth.length);}
		else if (imageWidth.substring(0, 1) == 't' || imageWidth.substring(0, 1) == 'p') {imageWidth = imageWidth.substring(1, imageWidth.length);}
		else if (imageWidth.substring(0, 2) == 'bb') {imageWidth = imageWidth.substring(2, imageWidth.length);}
		tmp = tmp[1].split('.');
		imageHeight = tmp[0];
	}
	
	image = imageURL + 'adimages/' + image;
	$('#ad-view-image').attr('src', image).attr('width', imageWidth).attr('height', imageHeight);
	if (alt != '') {
		$('#ad-view-image').attr('alt', alt);
	} else {
		$('#ad-view-image').attr('alt', imageWidth + 'x' + imageHeight);
	}
	
	// reload graphs
	var ad_type = $("#input-search input[name='type_item']:checked").val();	
	var zone_id = $("#input-search-items input[name='" + ad_type + "-radio']:checked").val();
	reloadCharts(zone_id);
	
}

function checkRadio(objid) {
	if (objid > 0) {
		$('#input-search input').attr('checked', false);
		$('#type-item-' + objid).attr('checked', true);
	}
}

function zone_select (action) {
	if (action == 'hide') {
		$('#zone-search').hide();
		$('#zone-search').attr('width', '1');
	} else if (action == 'show') {
		$('#zone-search').show();
		$('#zone-search').attr('width', '100%');
	}
} 

function typeSwitch(obj) {
	var objRegExp  = /^type\-item\-(?:name\-)?(\d+)$/;
	var objid = objRegExp.exec($(obj).attr('id'));
	var mtc = objid[1];
	
	$('#input-search span').each(function(){
		if (objRegExp.test($(this).attr('id'))) {
			var itemid = objRegExp.exec($(this).attr('id'));
			if (itemid[1] == objid[1]) {
				$(this).attr('class', '').addClass('ad_type_selected');
			} else {
				$(this).attr('class', '').addClass('ad_type');
			}
		}
	});
	// if ($(obj).attr('id') == 'type-item-1' || $(obj).attr('id') == 'type-item-name-1') {
	if (mtc == 1 || mtc == 5) {
		zone_select('show');
		$('#type-popunder').hide();
		$('#type-banner').hide(); 
		$('#type-banner_big').hide(); 
		$('#type-invideo_image').hide();
		$('#ad-divider').addClass('ad_divider');
		
		if (mtc == 1) {
			$('#type-text').show();
			$('#type-text input').each(function(){
			    if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
			});
		} else {
			$('#type-invideo_text').show();
			$('#type-invideo_text input').each(function(){
			    if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
			});
		}
		
		
		
	// } else if ($(obj).attr('id') == 'type-item-2' || $(obj).attr('id') == 'type-item-name-2') {
	} else if (mtc == 2 || mtc == 6 || mtc == 8) {
		zone_select('show');
		$('#type-text').hide(); 
		$('#type-invideo_image').hide();
		$('#type-invideo_text').hide();
		$('#type-popunder').hide(); 
		$('#type-banner').hide();
		$('#type-banner_big').hide();
		$('#ad-divider').addClass('ad_divider');
		
		if (mtc == 2) {
			$('#type-banner').show();
			$('#type-banner input').each(function(){
				if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
			});
		} else if (mtc == 8) {
			$('#type-banner_big').show();
			$('#type-banner_big input').each(function(){
				if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
			});
		} else {
			$('#type-invideo_image').show();
			$('#type-invideo_image input').each(function(){
				if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
			});
		}
	} else {  // 4 7
		zone_select('hide');
		$('#type-text').hide(); 
		$('#type-invideo_text').hide();
		$('#type-banner').hide(); 
		$('#type-banner_big').hide(); 
		$('#type-invideo_image').hide();
		$('#type-popunder').show();
		
		$('#ad-divider').removeClass('ad_divider');
		
		// $('#buy-button').hide();
		$('#type-popunder input').each(function(){
			if (typeof($(this).attr('checked')) != 'undefined' && $(this).attr('checked') == true) {checkRadio(mtc); changeAdView($(this).attr('id'), $(this).attr('value'));}
		});		
	}
	
	
}

function reloadCharts (zone_id) {
	$("td[id*='chart-']").each(function(){
		var obj = $(this).attr('id');
		var type = obj.split('-')[1];
		var stats_id = obj.split('-')[2];
		
		var swf = '';
		if (type == 'line') {swf = "linechart";}
		if (type == 'pie') {swf = "piechart";}
		
		chartEmpty(obj);
		chartCreate(obj, swf, stats_id, zone_id, type);
	});
	
	retrieveZoneInfo(zone_id);
}

function makeLoading () {
	$('#info-show').hide();
	$('#info-loading').show();
	/*
	$('#info-show-t').hide();
	$('#info-loading-t').show();
	*/
}

function removeLoading() {
	$('#info-show').show();
	$('#info-loading').hide();
	/*
	$('#info-show-t').show();
	$('#info-loading-t').hide();
	*/
}

function retrieveZoneInfo(zone_id) {
	makeLoading();
	var callback = function() {removeLoading();}
	
	var p = 'action=getZoneInfo';
	p += '&zone_id=' + zone_id;
	ajaxCall(ajURL, p, callback);
}

function setInfoValues(impr, visitors) {
	$("#more-info #info-show span").each(function(){
		if ($(this).attr('class') == 'e_impr') {$(this).empty().text(impr);}
		if ($(this).attr('class') == 'e_visitors') {$(this).empty().text(visitors);}
	});
	/*
	$("#more-info-t #info-show-t span").each(function(){
		if ($(this).attr('class') == 'e_impr') {$(this).empty().text(impr);}
		if ($(this).attr('class') == 'e_visitors') {$(this).empty().text(visitors);}
	});
	*/
}

function chartEmpty (obj) {
	$('#' + obj).empty();
}

function chartCreate (obj, swf, stats_id, zone_id, type) {
	var fo = new FlashObject("/static/default/swf/" + swf + ".swf", obj + "-chart", "395", "150", 0, "#ffffff");
		fo.addParam("wmode", "transparent");
		fo.addParam("menu", "false");
		fo.addParam("scale", "noscale");
		fo.addVariable("xmlurl", encodeURIComponent('/chart.php?type=' + type + '&stats_id=' + stats_id + '&zone_id=' + zone_id + '&conf_id=3&nc=1&p=1'));
		fo.addParam("salign", "lt");
		fo.addParam("quality", "high");
		fo.write(obj);
}

function searchData() {
	$('#search-button').hide();
	$('#search-button-searching').css('display', 'inline');
		
	callback = function() {
		$('#search-button-searching').hide();
		$('#search-button').css('display', 'inline');
	}
	
	var p = 'action=search';
	p += '&q=' + eI($('#search-query').val());
	ajaxCall(ajURL, p, callback);
}

function buyAds() {
	$('#buy-button').hide();
	$('#buy-button-processing').show();
		
	callback = function() {
		$('#buy-button-processing').hide();
		$('#buy-button').show();
	}
	
	var p = 'action=buy';
	//input[@name='option_layout']:checked").val()
	//alert($("#input-search input[name='type_item']:checked").val());
	//alert();
	var ad_type = $("#input-search input[name='type_item']:checked").val();
	p += '&ad_type=' + ad_type;
	p += '&zone_id=' + $("#input-search-items input[name='" + ad_type + "-radio']:checked").val();
	
	// ' + escape($('#search-query').val());
	//p += '&q=' + escape($('#search-query').val());
	ajaxCall(ajURL, p, callback);
	//alert($("#input-search-items input[name='" + ad_type + "-radio']:checked").val());
}

function buyAds2() {
	var ad_type = $("#input-search input[name='type_item']:checked").val();
	$('#h-zone-id').val($("#input-search-items input[name='" + ad_type + "-radio']:checked").val());
	$('#h-ad-type').val(ad_type);
	$('#target').submit();
}

function goBuy (zoneID, adType) {
	document.location.href = '/?c=a_target_ads&new=1&zone_id=' + zoneID + "&ad_type=" + adType;
}
