// This file contains all the customization options and iframe code generation for cyclur
//this is the main changeWidget function that refreshes the iframe code
function changeWidget(){
		var height= $('#h').val();
		var width = $('#w').val();
		var list = $('#list').val();
//set max width to 300 and min to 200
	if ($('#w').val() <= 300 && $('#w').val() >= 200){
			width = $('#w').val();
	} else {
		$('#w').attr('value', 300);
		width = 300;
	}
//setting minimum height 250 and max 450
//	if ($('#h').val() <= 450 && $('#h').val() >= 250){
//			height = $('#h').val();
//	} else {
//	$('#h').attr('value', 250);
//	height = 250;
//	}
//change the height of the widget depending on the no. of items listed. called from the changeWidget function
	if (list > 10){
		if (list==11){
			$('#h').attr('value', 270);
			height=270;
		} else if (list==12){
			$('#h').attr('value', 290);
			height=290;
		}else if (list==13){
			$('#h').attr('value', 310);
			height=310;
		}else if (list==14){
			$('#h').attr('value', 330);
			height=330;
		}else if (list==15){
			$('#h').attr('value', 350);
			height=350;
		}else if (list==16){
			$('#h').attr('value', 370);
			height=370;
		}else if (list==17){
			$('#h').attr('value', 390);
			height=390;
		}else if (list==18){
			$('#h').attr('value', 410);
			height=410;
		}else if (list==19){
			$('#h').attr('value', 430);
			height=430;
		}else if (list==20){
			$('#h').attr('value', 450);
			height=450;
		} else if(list <10){
			$('#h').attr('value', 250);
			height=250;
			$('#list').attr('value', 10);
			list=10;
		}else if(list >20){
			$('#h').attr('value', 250);
			height=250;
			$('#list').attr('value', 10);
			list=10;
		}else {
		height = 250;
		}
	}
//setting minimum height 250 and max 450
		var queryParams = $('#widgetform').find(':input[name]').serialize().replace(/#/g, '').replace(/%23/g, '');
		var iframesrc = 'http://www.cyclur.com/cyclur.php?' + queryParams;
		$('#cyclur').attr('height', height);
		$('#cyclur').attr('width', width);
		$('#cyclur').attr('src', iframesrc);
		$('#iframetag').text($.trim($('#widgetpreview center').html()).replace(/ id="rsswidget"/,'').replace(/&amp;/g, '&').replace(/%3A/g, ':').replace(/%2F/g, '/'));
}

$(document).ready(function(){
var colorPicked =0;
	$.ajaxSetup ({
		cache: false
	});

	$('#refresh').click(function() {
	   document.getElementById('cyclur').src = document.getElementById('cyclur').src; 
	   return false;
	});

	$(':input').change(function() {
		changeWidget();
	}).triggerHandler('change');  
	$('.pickerTable').click(function() {
		changeWidget();
	}).triggerHandler('change');  

	
	$('.formhalf:input[type=text]').filter(':focus').keyup(function(e) {
		if(e.keyCode == 13) {
		$(this).triggerHandler('change');
		}
	});

	$('form').submit(function(){
			return false;
		});


//	load() functions
	var ajax_load = "<img class='loading' src='images/loader.gif' alt='loading...' />";
	var ajax_button = "<a href='#'><img src='images/arrow.png' /></a>";
	var loadUrl = "scripts/utils/validatefeed.php";
	$(".feedsubmit, .feedsubmit2").click(function(){
		var feedurl = $('#feedurl').val();
		$(".feedsubmit, .feedsubmit2").html(ajax_load);
		$(".feedresponse").load(
			loadUrl, 
			"url=" + feedurl + "", 
			function(responseText){
				if (responseText == "Congrats, your RSS feed was successfully Cycled, now you may continue customizing the theme below."){
					$('#url').val($('#feedurl').val());
					changeWidget();
				} else {
					
				}
				$('.feedresponse').css("visibility","visible");		
				$(".feedsubmit, .feedsubmit2").html(ajax_button);
			});
			return false;
	});

	$('#feedurl:input[type=text]').click(function(){
		$('.feedresponse').html("eg. http://www.gamedibs.com/dibs/feed");
	});

});


