var base_url = 'http://widget.sanook.com/rss-diy/';
var swf_url = 'http://widget.sanook.com/'+'swf/main.swf';
	$(document).ready(function(){
		//rss url
		$('#receive1').click(function(){
			$('#rssUrl').attr('disabled','disabled');
			$('#rssUrl').attr('value','ระบุ URL');
			$('#service').val('sanook');
		})
		$('#receive2').click(function(){
			$('#rssUrl').attr('disabled','');
			$('#subCate').attr('style','display:none');
			$('#r-option1').val('เลือกหมวด');
		})
		$('#rssUrl').click(function(){
			$('#rssUrl').attr('value','');
			$('#rssUrl').focus();
			$('#rssUrl').blur(function(){
				$('#feedsUrl').val($('#rssUrl').val());
				$('#service').val('etc');
				reloadFlash();
			});
		});

		//news topic
		$('#r-option1').change(function(){
			if($('#r-option1').val()!='เลือกหมวด'){
				$.ajax({
					url :'/rss-diy/rss_subCate.php',
					type : 'POST',
					data : 'cateID='+$('#r-option1').val(),
					success : function(text){
						if(text!=''){
							//alert(text);
							$('ul #subCate').html(text);
							$('#subCate').attr('style','');
							checkSubCate($('ul #subCate input').val());
							//alert($('ul #subCate input').val());
						}
					}
				});
			}else{
				$('#subCate').attr('style','display:none');
			}
			return false;
		});

		//กรณีที่มี id ของ category มาด้วย
		if($('#r-option1').val()!='เลือกหมวด'){
			$.ajax({
				url :'/rss-diy/rss_subCate.php',
				type : 'POST',
				data : 'cateID='+$('#r-option1').val()+'&newsSub='+$('#newsSub').val(),
				success : function(text){
					if(text!=''){
						//alert(text);
						$('ul #subCate').html(text);
						$('#subCate').attr('style','');
					}
				}
			});
			if($('#newsSub').val()!=''){
				checkSubCate($('#newsSub').val());
			}
		}
		
		//Load boxsize Function
		$('#boxsize ul li a img').click(function(){
			$('#boxsize ul li a .selected').removeClass('selected');
			$(this).attr('class','selected');
			
			if($(this).attr('rel')=='size3'){
				$('#fixsize').attr('class','custom-size');
				//width and height
				$('#confirmsize').click(function(){
					if($('#size-width').val()<180||$('#size-width').val()>400||$('#size-height').val()<300||$('#size-height').val()>600){
						alert('ขนาดกล่องไม่เหมาะสม');
					}else{
						$('#sizeX').val($('#size-width').val());	
						$('#sizeY').val($('#size-height').val());	
						//window.setTimeout("reloadFlash()",'5000');
						reloadFlash();
					}
					return false;
				});
				//$('#size-width').focus();
			}else{
				$('#fixsize').attr('class','custom-size hide');
				//width and height
				$('#sizeX').val($(this).attr('rel')) ;
				$('#sizeY').val('450') ;
				reloadFlash();
			}
			
		});

		//theme
		$('#themeSelect ul.list-show-item li a img').click(function(){
			$('#boxcolor').attr('style','display:none;');
			$('#themeSelect ul li a img').removeClass('selected');
			$(this).attr('class','selected');
			$('#theme').val('0x'+$(this).attr('rel')) ;
			$('#pickerStatus').val('0');
			reloadFlash();
			//alert($(this).attr('rel'));
		});

		//theme color picker
		$('#themeColor').click(function(){
			if($('#pickerStatus').val()=='1'){
				$('#boxcolor').attr('style','display:none;');
				$('#pickerStatus').val('0');
				//reloadFlash();
			}else{
				$('#boxcolor').attr('style','');
				$('#colorpicker').farbtastic('#color');
				$('#pickerStatus').val('1');
				reloadFlash();
			}
		});

		//display type (slide or not)
		$('#displaySelect').change(function(){
			$('#displayType').val($(this).val());
			reloadFlash();
		});

		// num Topic
		$('#numTopic').change(function(){
			$('#totalTopic').val($(this).val());
			reloadFlash();
		});
	});

//check Sub catagory
function checkSubCate(id){
	var newsUrl;
	switch(id){
		case "35":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hot.entertain.xml";break;
		case "4":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hot.variety.xml";break;
		case "5":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hot.technology.xml";break;
		case "6":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hot.sport.xml";break;
		case "8":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.index.xml";break;
		case "9":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.politic.xml";break;
		case "10":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.crime.xml";break;
		case "11":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.world.xml";break;
		case "13":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.economic.xml";break;
		//case "15":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/star.gossip.xml";break;
		case "16":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/movie.news.xml";break;
		case "17":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/movie.now.xml";break;
		case "18":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/movie.coming.xml";break;
		case "19":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/movie.drama.xml";break;
		case "20":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/music.news.xml";break;
		case "22":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.news.xml";break;
		case "23":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/game.news.xml";break;
		case "343":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/game.hit.xml";break;
		case "344":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/game.tournament.xml";break;
		case "25":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/horoscope.index.xml";break;
		case "26":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/travel.index.xml";break;
		case "27":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/women.beauty.xml";break;
		case "28":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sex.index.xml";break;
		case "29":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/women.mom.xml";break;
		case "30":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/campus.teenzone.xml";break;
		//case "34":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/football.index.xml";break;
		case "3":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hot.news.xml";break;

		case "334":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/news.entertain.xml";break;
		case "335":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.world.xml";break;
		case "336":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.scoop.xml";break;
		case "337":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.football.xml";break;
		case "338":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.result.xml";break;
		case "339":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.golf.xml";break;
		case "340":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.tennis.xml";break;
		case "341":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.boxing.xml";break;
		case "342":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/sport.f1.xml";break;

		//case "345":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.mobile.index.xml";break;
		case "346":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.computer.index.xml";break;
		case "347":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.digital_camera.index.xml";break;
		case "348":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.gadget.index.xml";break;
		case "349":newsUrl = "http://rssfeeds.sanook.com/rss/feeds/sanook/hitech.home_electronic.index.xml";break;
	}
	$('#feedsUrl').val(newsUrl);
	reloadFlash();
	//alert(newsUrl);
}

//Get Code
function checkChange(){
	if(document.getElementById('chkPost').checked==false){
		alert('กรุณายอมรับเงื่อนไขการใช้บริการ');
		return false;
	}
	if($('#r-option1').val()=='เลือกหมวด'&&$('#rssUrl').val()=='ระบุ URL' ){
	   alert('กรุณาเลือกรูปแบบการรับ RSS');
	   return false;
	}
	if($('#rssUrl').val()==''){
	   alert('กรุณากรอก URL');
	   return false;
	}
	if($('#numTopic').val()=='0'){
	   alert('กรุณาเลือกจำนวนหัวข้อข่าว');
	   return false;
	}
	if($('#displaySelect').val()=='0'){
	   alert('กรุณาเลือกรูปแบบการแสดงผล');
	   return false;
	}
	if($('#fixsize').attr('class')=='custom-size'){
		if($('#size-width').val()<180||$('#size-width').val()>400||$('#size-height').val()<300||$('#size-height').val()>600){
			alert('ขนาดกล่องไม่เหมาะสม');
			return false;
		}else{
			$('#sizeX').val($('#size-width').val());	
			$('#sizeY').val($('#size-height').val());	
		}
	}
	if($('#r-option1').val()=='เลือกหมวด'){
		$('#feedsUrl').val($('#rssUrl').val());
	}
	data = getRssVal();
	window.location="http://widget.sanook.com/rss-diy/rss_process?"+data;
 }

// Genarate Flash on the File
function reloadFlash()
{
	
	var allInput = getRssVal();
	//alert(allInput);
	var embed =	document.createElement('embed');
	$(embed).attr('type', 'application/x-shockwave-flash');
	$(embed).attr('id', 'widget_rss');
	//$(embed).attr('src', base_url + 'swf/main.swf');
	$(embed).attr('src',swf_url);
	$(embed).attr('bgcolor', '#FFFFFF');
	$(embed).attr('wmode', 'transparent');
	$(embed).attr('flashvars', 'xmlPath=' + base_url + 'rss_builtXML.php?' + Base64.encode(allInput));
	$(embed).attr({ 'width': 400, 'height': 600 });
	$('#rss_show').empty().html(embed);
}

// Get Current Data
function getRssVal()
{
	var rand = Math.random() * 100;
	$('#randNo').val(rand);
	var allInput =	 $('form#rss_val').find('input:hidden').serialize();
	return allInput;
}
