var defaultIndex;

function GetURL()
{
	var path = window.location.pathname;
	
	path = path.substring(0, path.lastIndexOf('/')+1)
	
	//return(window.location.protocol+'//'+window.location.host+path);
	return(window.location.protocol+'//'+window.location.host+'/');
}

function GetFullURL()
{
	var path = window.location.pathname;
	
	path = path.substring(0, path.lastIndexOf('/')+1)
	
	return(window.location.protocol+'//'+window.location.host+path);
}

function GetSchedule(row_id)
{
	$.post(GetURL()+'ajax/get_schedule.php', {title: $('#show_name'+row_id).text().replace(/^\s+/, '')}, function(data){

		$('#rating'+row_id).html(data.datetime);
		
		}, "json");
}

function AdActiveToggle(obj)
{
	$.post(GetURL()+'ajax/ad_active_toggle.php', {ad_ID: $(obj).attr('id').substr(6)}, function(data){
		
		if(data.status == 0)
		{
			$(obj).removeClass('link_active');
			$(obj).addClass('link_inactive');
			$(obj).attr('title', 'Active this Ad');
			$(obj).text('Inactive');
		}
		else
		{
			$(obj).removeClass('link_inactive');
			$(obj).addClass('link_active');
			$(obj).attr('title', 'Disactive this Ad');
			$(obj).text('Active');
		}
		
		}, "json");
	
	return(false);
}

function AdClicked(obj)
{
	$.post(GetURL()+'ajax/ad_clicked.php', {ad_ID: $(obj).attr('id').substr(8)}, function(){
	});
}

function CheckPromoCode()
{
	$.post(GetURL()+'ajax/check_promo_code.php', {code: $('#promo_code').val()}, function(data){
		if(data.status == 1)
		{
			$('#base_cost').val(data.base_cost);
			CountTotalCost();
		}
	}, "json");
}

function DirectoryTowns()
{
	$('#towns').autocomplete(GetURL()+'ajax/get_towns.php', {
		minChars: 0,
		max: 20,
		autoFill: true,
		matchContains: false,
		dataType: 'json',
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.town+'',
					result: row.town+''
				}
			});
		},
		formatItem: function(item) {
			return item.town;
		} 
	});
			
}


function WedSimpleCheckUser()
{
	
	var userID = 1;
	
	$.post(GetURL()+'ajax/wedsimple_is_user.php', {u: userID}, function(data){
		$('#response').append(data);
	}, "json");
	/*
	//alert(''+$.md5(userID)+' - typ: '+typeof($.md5(userID)));
	
	var options = {
		url: 'http://tools.weddingtv.com/wsPlatform/account/'+userID,
		type: 'GET',
		dataType: 'json',

		beforeSend: function(xhr) {
			//xhr.setRequestHeader('Accept', 'text/json');
			//xhr.setRequestHeader('Hash', ''+$.md5(userID));
		}/*,
		success: function(data) {
			alert(data);
			//$('#response').text(data);
		}
	};

	$.ajax(options).send();
	//*/
}


function Rating(section_id)
{
	defaultIndex = parseInt($('#rating').attr('src').substr($('#rating').attr('src').length-5, 1));
	
	$('#rating').mousemove(function(e){
		var pos = $(this).offset();
		var x = parseInt((e.pageX-pos.left)/13)+1;
			
		$(this).attr('src', rating_imgs[x].src);
	});

	//
	$('#rating').mouseout(function(e){
		$(this).attr('src', rating_imgs[defaultIndex].src);
	});
	
	$('#rating').click(function(e) {
		var pos = $(this).offset();
		var x = parseInt((e.pageX-pos.left)/13)+1;
			
		$(this).attr('src', rating_imgs[x].src);
			
		$(this).unbind('click mouseout mousemove');
			
		// send post request
		$.post(GetURL()+'ajax/rate_it.php', {obj: 'section', id: section_id, rate: parseInt($('#rating').attr('src').substr($('#rating').attr('src').length-5, 1))});
	});
}


function AddComment(section_id)
{
	$('#save').click(function() {
		
		if($('#comment').length)
		{
			// sending request
			$.post(GetURL()+'ajax/add_comment.php', {obj: 'section', id: section_id, comment: $('#comment').val()}, function(data) {
				
				//$('#comment').attr('disable', 'disable');
				// clone first element
				
				var new_comment = $('div[class^="comment_row"]:first').clone();
				
				if(new_comment.hasClass('comment_row1'))
				{
					new_comment.removeClass('comment_row1');
					new_comment.addClass('comment_row2');
				}
				else
				{
					new_comment.removeClass('comment_row2');
					new_comment.addClass('comment_row1');
				}
				
				jQuery.each(new_comment.children().children('span'), function(i, el) {

					switch($(el).attr('class'))
					{
						case 'comment_username':
							$(el).text(data.username);
							break;
							
						case 'comment_date':
							$(el).text('('+data.date+')');
							break;
							
						case 'comment_text':
							$(el).text($('#comment').val());
							break;
					}
					
				});
				
				//var current_class = new_comment.removeClass();
				new_comment.prependTo('div#comments');
				
				//alert($('div[class^="comment_row"]:first').attr('class'));
				
				//$('#comments').prepend($('#comments > div.comment_row:first').html());
			}, "json");
		}
		
		return false;
	});
}


function GetSectionList(s, subs, type, count, page)
{
	// type = {'most_viewed', 'most_popular', 'latest', 'related'}
	// count = 9 
	// page = 1
	
	var c_name = 'tab-' + s + ((subs == '') ? '' : '-' + subs);
	var c_val = $.cookie(c_name);

	if(c_val != 'null')
	{
		c_val = c_val.split(';');
		c_val[1] = page;
		$.cookie(c_name, c_val.join(';'), {path: '/', expires: '0'});
	}
	
	if(typeof(count) == 'undefined')
		count = 9;
	else
		count = parseInt(count);
		
	if(typeof(page) == 'undefined')
		page = 1;
	else
	{
		page = parseInt(page);
		
		if(page < 1)
			page = 1;
	}
		
	$.post(GetURL()+'ajax/get_section_list.php', {section: s, subsection: subs, type: type, count: count, page: page}, function(data) {

		var obj1, obj2, obj3;
		
		$('#tabs-'+type).empty();

		for(var i=1; i<=data[0].returned; i++)
		{
			//<div class="item_line">
			//	<div class="hearts">
			//		<img src="files/img/ratings/rating3.jpg" width="65" height="12" />
			//	</div>
			//	<div class=""></div>
			//</div>
			//<div class="item_line">
			//	<div class="video_icon"></div>
			//	<div class="item_views">views: 123</div>
			//</div>
			//<div class="item_desc">
			//	Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sodales varius velit, at interdum quam ullamcorper nec.
			//</div>
			//
			var url = GetURL()+''+s+'/'+data[i].subsection+'/'+data[i].id+'/'+data[i].data.title_url;
			var div_item = $.create('div', {'class': 'list_item'});
			
			// img
			obj1 = $.create('a', {'href': url, 'class': 'item_img', 'alt': ''+data[i].data.title, 'title': ''+data[i].data.title});
			$(obj1).append($.create('img', {'src': data[i].data.img+''+data[i].data.unique_id+'_m.jpg', 'width': '130', 'height': '98', 'alt': ''+data[i].data.title}));
			$(div_item).append($(obj1));
			
			// title link
			$(div_item).append($.create('a', {'href': url, 'class': 'item_title', 'alt': ''+data[i].data.title, 'title': ''+data[i].data.title}, [''+data[i].data.title]));
			
			// episode
			if(data[i].data.episode && data[i].data.episode.length > 0)
				$(div_item).append($.create('span', {}, ['ep. '+data[i].data.episode]));
			
			// hearts
			obj1 = $.create('div', {'class': 'item_line'});
			obj2 = $.create('div', {'class': 'hearts'});
			
			var rating = (data[i].votes > 0) ? Math.round(data[i].total/data[i].votes) : 0;
			
			obj3 = $.create('img', {'src': 'files/img/ratings/rating'+rating+'.jpg', 'width': '65', 'height': '12'});
			
			$(obj2).append($(obj3));
			$(obj1).append($(obj2));
			$(div_item).append($(obj1));
			
			// type & votes
			obj1 = $.create('div', {'class': 'item_line'});
			$(obj1).append($.create('div', {'class': data[i].content_type+'_icon'}));
			$(obj1).append($.create('div', {'class': 'item_views'}, ['views: '+data[i].views]));
			$(div_item).append($(obj1));
			
			// desc
			$(div_item).append($.create('div', {'class': 'item_desc'}, [''+data[i].data.desc_short]));
			
			$('#tabs-'+type).append($(div_item));
			
			if(i%3 == 0)
				$('#tabs-'+type).append($.create('div', {'class': 'clear'}));
		}
		
		// PAGING
		var pages = Math.ceil(data[0].max/count);
		obj1 = $.create('div', {'class': 'paging_items'});
				
		// first & prev
		if(page > 1)
		{
			// first
			obj2 = $.create('a', {'href': '#', 'class': 'paging_prev_link'}, ['« first']);
			$(obj2).click(function() {
				GetSectionList(s, subs, type, count, 1);
				
				return false;
			});
			
			$(obj1).append($(obj2));
			
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// prev
			obj2 = $.create('a', {'href': '#', 'class': 'paging_prev_link'}, ['‹ prev']);
			$(obj2).click(function() {
				GetSectionList(s, subs, type, count, page-1);
				
				return false;
			});
			
			$(obj1).append($(obj2));
			
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
		}
		else
			$(obj1).append($.create('div', {'class': 'paging_empty'}));
		
		// page X of Y
		$(obj1).append($.create('div', {'class': 'paging_info'}, ['Page '+page+' of '+pages]))
		
		// next & last
		if(page < pages)
		{
			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// next
			obj2 = $.create('a', {'href': '#', 'class': 'paging_next_link'}, ['next ›']);
			//$(obj2).unbind('click');
			
			$(obj2).click(function() {
				GetSectionList(s, subs, type, count, page+1);
				
				return false;
			});
			
			$(obj1).append($(obj2));

			// breaker
			$(obj1).append($.create('div', {'class': 'paging_break'}));
			
			// last
			obj2 = $.create('a', {'href': '#', 'class': 'paging_next_link'}, ['last »']);
			//$(obj2).unbind('click');
			
			$(obj2).click(function() {
				GetSectionList(s, subs, type, count, pages);
				
				return false;
			});
			
			$(obj1).append($(obj2));
		}
			
		$('#tabs-'+type).append($(obj1));
		
		$('#tabs-'+type).append($.create('div', {'class': 'clear'}));

	}, 'json');
}

