/**
 * @author .DeV!L
 */

var core_config = {
	server_script: 'http://bestofwebdesing.com/aff_shop_srv/remote/get.php'
};
 
$(function(){
	$("ul.cats a").click(function(e){
		var cat = $(this).attr('cat');
		checkMainView();
		view("#main_frame", {'cat': cat, 'paging' : 1, 'info': 1});
	});
	
	$("ul.types a").click(function(e){
		var type = $(this).attr('type');
		checkMainView();
		view("#main_frame", {'type': type, 'paging' : 1, 'info': 1});
	});
	
	$("#search").keypress(function(e){
		if (e.which == 13){
			$("#search_btn").click();
		}
	});
	
	$("#search_btn").click(function(e){
		var data = {'paging': 1};
		
		var type = parseInt($("#type").val());
		if (type != NaN && type > 0){
			data.type = type;	
		}
		
		var cat = parseInt($("#cat").val());
		if (cat != NaN && cat > 0){
			data.cat = cat;	
		}
		
		var key = $("#key").val();
		if (key != ' - any -' && key != ''){
			data.key = key;
		}
		
		var number = parseInt($("#number").val());
		if (number != NaN && number > 0){
			data.number = number;	
		}
		
		checkMainView();
		view("#main_frame", data);
	});
	
	$(".l").click(function(){
		$("#menu a").removeClass('active');
		checkMainView();
		$(this).addClass('active');
		$("#main_frame").attr('src', $(this).attr('val'));
		return false;
	});
	
	$(".support").click(function(){
		window.open("", "support");
		return false;
	});
	
	//init
	switch (getPage())
	{
		case '':
		case 'index.html':
		
			addFrame("#frame_view");
			view("#main_frame", {});
		
			break;
			
		case 'templates.html':
		
			/*addFragment("#frame_view", {'title': 'Flash Templates', 'name': 'flash', 'type': 12, 'cols' : 2, 'rows': 2, 'link_title': 'View all flash templates'});
			addFragment("#frame_view", {'title': 'Flash CMS Templates', 'name': 'flash_cms', 'type': 19, 'cols' : 2, 'rows': 2, 'link_title': 'View all flash cms templates'});
			addFragment("#frame_view", {'title': 'CSS Templates', 'name': 'css', 'type': 21, 'cols' : 2, 'rows': 2, 'link_title': 'View all css templates'});*/
			
			break;
	}
	
})

function addFrame(obj)
{
	$(obj).append('<iframe width="'+config.width+'" height="'+config.heigth+'" src="" frameborder="0" id="main_frame"></iframe>');
}

function addFragment(obj, data)
{
	$(obj).append('<iframe width="'+config.width+'" height="'+data.rows * 205+'" src="" frameborder="0" id="f_'+data.name+'"></iframe>');
	view("#f_"+data.name, data);
}

function viewAll(f)
{
	checkMainView();
	view("#main_frame", {'type': $(f).attr('t'), 'paging' : 1, 'info': 1});	
}

function checkMainView()
{
	var mf = $('#main_frame');
	if (mf.length == 0)
	{
		$("#frame_view").html('');
		addFrame("#frame_view");
	}
}
 
function view(obj, data)
 {
	var query = new Array();
 	if (data.cat){
		query.push('cat='+parseInt(data.cat));
	}
	
 	if (data.type){
		query.push('type='+parseInt(data.type));
	}
	
	if (data.key){
		query.push('key='+data.key);
	}
	
	if (data.number){
		query.push('number='+parseInt(data.number));
	}
	
	if (data.paging){
		query.push('paging=1');
	}
	
	if (data.info){
		query.push('info=1');
	}
	
	if (data.cols){
		query.push('cols='+data.cols);
	}else{
		query.push('cols='+config.cols);
	}
	
	if (data.rows){
		query.push('rows='+data.rows);
	}else{
		query.push('rows='+config.rows);
	}
	
	query.push('aff='+config.aff_id);
	
	query.push('style='+encodeURI(config.styles));
	
	var mf = $(obj);
	mf.attr('src', core_config.server_script+'?'+query.join('&'));
}

function getPage(){
	var str = window.location.pathname;
	var ind = str.lastIndexOf('/')+1;
    return str.substr(ind);
}