jQuery(document).ready(function($) {
	
	cocktail_offset = 0;
	shababiah_offset = 0;
	sms_offset = 0;
	shababiah_tab = 13;
    itemsNumber = 4;
    current = 1;
    nextI = 0;

    $('.ui-tabs-panel').hide();
    $('#featured .ui-tabs-title').hide();
    $('#fragment-1').fadeIn();
    $('#nav-title-1').fadeIn();
    timeoutID = window.setInterval('showNextItem()', 6000);
    $('#featured .ui-tabs-nav-item').hover(
			function() {
				window.clearInterval(timeoutID);
				hideItems();
				showItem($(this).attr('id').replace("nav-fragment-", ""));
			},
			function() {
				clearInterval(timeoutID);
				timeoutID = window.setInterval('showNextItem()', 6000);
			}
	);
    a = 0;
	
	//search box
	$('#s').focus(function(){
		if(($(this).val()) == 'ابحث') {$(this).val(''); }
	});
	$('#s').focusout(function(){
		if(($(this).val()) == '') {$(this).val('ابحث'); }
	});
	
	// Cocktail slide
	try {
		$('#cocktail ul').after('<img class="arrow_left" src="' + template_url + '/images/arrow-left.jpg" />');
		$('#cocktail h3').after('<img class="arrow_right disabled" src="' + template_url + '/images/arrow-right-disabled.jpg" />');

		$('#cocktail .arrow_left').click(function() {
			var data = {
				type: 'POST',
				action: 'ajax_data',
				numberofposts: 4,
				cat : 3204,
				offset: ++cocktail_offset
			};
			jQuery.post("http://ainnews.net/wp-admin/admin-ajax.php", data, function(response) {
				$('#cocktail .recentnews-image a').each(function(index) {
					$(this).attr('href', response[index].link);
					$(this).attr('title', response[index].title);
					$(this).children('img').replaceWith(response[index].img);
					$(this).children('p').html(response[index].title);
				});
			});
			$('#cocktail .arrow_right').attr('src', template_url + '/images/arrow-right.jpg');
			$('#cocktail .arrow_right').removeClass('disabled');
		});
		$('#cocktail .arrow_right').click(function() {
			if(cocktail_offset > 0) {
				var data = {
					type: 'POST',
					action: 'ajax_data',
					numberofposts: 4,
					cat : 3204,
					offset: --cocktail_offset
				};
				jQuery.post("http://ainnews.net/wp-admin/admin-ajax.php", data, function(response) {
					$('#cocktail .recentnews-image a').each(function(index) {
						$(this).attr('href', response[index].link);
						$(this).attr('title', response[index].title);
						$(this).children('img').replaceWith(response[index].img);
						$(this).children('p').html(response[index].title);
					});
				});
				if(cocktail_offset < 1 ) {
					$('#cocktail .arrow_right').attr('src', template_url + '/images/arrow-right-disabled.jpg');
					$('#cocktail .arrow_right').addClass('disabled');
				}
			} else {
					return false;
			}
		});
	} catch(e) {}
	
	// shababiah
	try {
		var shababiah_menu = '<div class="category-head-menu">';
		var shababiah_menu = shababiah_menu + '<a href="javascript:shababiah_AJAX(\'13\')"><div class="category-head-item category-head-item-selected">رياضة</div><div class="category-head-item-end category-head-item-end-selected"></div></a>';
		var shababiah_menu = shababiah_menu + '<a href="javascript:shababiah_AJAX(\'19\')"><div class="category-head-item">شباب وجامعات</div><div class="category-head-item-end"></div></a>';
		var shababiah_menu = shababiah_menu + '<a href="javascript:shababiah_AJAX(\'6\')"><div class="category-head-item">فن و ثقافة</div><div class="category-head-item-end"></div></a>';
		var shababiah_menu = shababiah_menu + '<a href="javascript:shababiah_AJAX(\'10\')"><div class="category-head-item">سيارات</div><div class="category-head-item-end"></div></a>';
		var shababiah_menu = shababiah_menu + '<a href="javascript:shababiah_AJAX(\'29\')"><div class="category-head-item">تكنولوجيا</div><div class="category-head-item-end"></div></a>';
		var shababiah_menu = shababiah_menu + '</div>';
		$('#shababiah h3').after(shababiah_menu);
		
		$('#shababiah .category-head-menu').after('<img class="arrow_up" src="' + template_url + '/images/arrow-up-disabled.jpg" />');
		$('#shababiah .recentnews-image-warp').after('<img class="arrow_down" src="' + template_url + '/images/arrow-down.jpg" />');
		
		$('#shababiah .arrow_up').click(function() {
			if(shababiah_offset > 0) {
				shababiah_AJAX(shababiah_tab, 'up');
			} else {
				return false;
			}
			if(shababiah_offset < 1 ) {
				$('#shababiah .arrow_up').attr('src', template_url + '/images/arrow-up-disabled.jpg');
				$('#shababiah .arrow_up').addClass('disabled');
			}
		});
		$('#shababiah .arrow_down').click(function() {
			shababiah_AJAX(shababiah_tab, 'down');
			$('#shababiah .arrow_up').attr('src', template_url + '/images/arrow-up.jpg');
			$('#shababiah .arrow_up').removeClass('disabled');
		});
		

	} catch (e) {}
	
	// people news
	try {
		var people_news_menu = '<div class="category-head-menu">';
		var people_news_menu = people_news_menu + '<a href="javascript:people_news_AJAX(\'24\')"><div class="category-head-item category-head-item-selected">وفيات</div><div class="category-head-item-end category-head-item-end-selected"></div></a>';
		var people_news_menu = people_news_menu + '<a href="javascript:people_news_AJAX(\'14199\')"><div class="category-head-item">مناسبات</div><div class="category-head-item-end"></div></a>';
		var people_news_menu = people_news_menu + '</div>';
		$('#people-news h3').after(people_news_menu);

	} catch (e) {}
	
	//sms
	try {
		$('#sms h3').after('<img class="arrow_up disabled" src="' + template_url + '/images/arrow-up-disabled.jpg" />');
		$('#sms').append('<img class="arrow_down" src="' + template_url + '/images/arrow-down.jpg" />');
		
		$('#sms .arrow_down').click(function() {
			sms_AJAX('down');
			$('#sms .arrow_up').attr('src', template_url + '/images/arrow-up.jpg');
			$('#sms .arrow_up').removeClass('disabled');
		});
		$('#sms .arrow_up').click(function() {
			if(sms_offset > 0) {
				sms_AJAX('up');
			} else {
				return false;
			}
			if(sms_offset < 1 ) {
				$('#sms .arrow_up').attr('src', template_url + '/images/arrow-up-disabled.jpg');
				$('#sms .arrow_up').addClass('disabled');
			}
		});
		
	} catch(e) {}
	
	//arrows & tabs effects
	try {
		$('.arrow_up').hover(
			function() {
				if(!($(this).hasClass('disabled'))) { $(this).attr('src', (template_url + '/images/arrow-up-hover.jpg')); }
			},
			function() {
				if(!($(this).hasClass('disabled'))) { $(this).attr('src', (template_url + '/images/arrow-up.jpg')); }
			}
		);
		$('.arrow_down').hover(
			function() {
				$(this).attr('src', (template_url + '/images/arrow-down-hover.jpg'));
			},
			function() {
				$(this).attr('src', (template_url + '/images/arrow-down.jpg'));
			}
		);
		$('.arrow_left').hover(
			function() {
				$(this).attr('src', (template_url + '/images/arrow-left-hover.jpg'));
			},
			function() {
				$(this).attr('src', (template_url + '/images/arrow-left.jpg'));
			}
		);
		$('.arrow_right').hover(
			function() {
				if(!($(this).hasClass('disabled'))) { $(this).attr('src', (template_url + '/images/arrow-right-hover.jpg')); }
			},
			function() {
				if(!($(this).hasClass('disabled'))) { $(this).attr('src', (template_url + '/images/arrow-right.jpg')); }
			}
		);

		$('.category-head-menu a').click(
			function() {
				$(this).parent().find('div').removeClass('category-head-item-selected');
				$(this).parent().find('div').removeClass('category-head-item-end-selected');
				$(this).children(':first').addClass('category-head-item-selected');
				$(this).children(':last').addClass('category-head-item-end-selected');
			}
		);
		
	} catch(e) {}
	
});


	function showNextItem() {
	    jQuery('#nav-fragment-'+current).removeClass('ui-tabs-selected');
	    jQuery('#nav-title-'+current).fadeOut();
	    jQuery('#fragment-'+current).fadeOut('slow', function() {
	       nextI = nextItem();
	       jQuery('#nav-fragment-'+nextI).addClass('ui-tabs-selected');
	       jQuery('#nav-title-'+nextI).fadeIn();
	       jQuery('#fragment-'+nextI).fadeIn();
	    });
	}
	
	function nextItem() {
	    current = parseInt(current);
	    if (current == itemsNumber) current = 0;
	    return ++current;
	}
	
	function showItem(curI) {
	       nextI = curI;
	       jQuery('#nav-fragment-'+nextI).addClass('ui-tabs-selected');
	       jQuery('#nav-title-'+nextI).fadeIn();
	       jQuery('#fragment-'+nextI).fadeIn();
	    current = curI;
	}
	
	function hideItems() {
	    jQuery('#featured .ui-tabs-nav-item').removeClass('ui-tabs-selected');
	    jQuery('#featured .ui-tabs-title').stop(true, true);
	    jQuery('#featured .ui-tabs-panel').stop(true, true);
	    jQuery('#featured .ui-tabs-title').css('display', 'none');
	    jQuery('#featured .ui-tabs-panel').css('display', 'none');
	}

	function shababiah_AJAX(category, move) {
		shababiah_tab = category;
		if(move == 'up') { 
			shababiah_offset--;
		} else if(move == 'down') {
			shababiah_offset++;
		} else {
			shababiah_offset = 0;
		}
		var data = {
			type: 'POST',
			action: 'ajax_data',
			numberofposts: 4,
			cat : category,
			offset: shababiah_offset
		};
		jQuery('#shababiah .recentnews-image').slideUp('slow');
		jQuery.post("http://ainnews.net/wp-admin/admin-ajax.php", data, function(response) {
			jQuery('#shababiah .recentnews-image a').each(function(index) {
				jQuery(this).attr('href', response[index].link);
				jQuery(this).attr('title', response[index].title);
				jQuery(this).children('img').replaceWith(response[index].img);
				jQuery(this).children('p').html(response[index].title);
			});
			jQuery('#shababiah .recentnews-image a img').load(function() {
				jQuery('#shababiah .recentnews-image').slideDown('slow');
			});
		});
	}
	
	function people_news_AJAX(category) {
		people_news_tab = category;

		var data = {
			type: 'POST',
			action: 'ajax_data',
			numberofposts: 3,
			cat : category
		};
		jQuery('#people-news ul li').fadeOut('slow');
		jQuery.post("http://ainnews.net/wp-admin/admin-ajax.php", data, function(response) {
			jQuery('#people-news ul li').fadeIn();
			jQuery('#people-news ul li a').each(function(index) {
				jQuery(this).attr('href', response[index].link);
				jQuery(this).attr('title', response[index].title);
				jQuery(this).html(response[index].title);
			});
		});
	}
	
	function sms_AJAX(move) {
		if(move == 'up') { 
			sms_offset--;
		} else if(move == 'down') {
			sms_offset++;
		} else {
			sms_offset = 0;
		}
		var data = {
			type: 'POST',
			action: 'ajax_data',
			numberofposts: 4,
			cat : 25,
			offset: sms_offset
		};
		jQuery('#sms ul li').slideUp('slow');
		jQuery.post("http://ainnews.net/wp-admin/admin-ajax.php", data, function(response) {
			jQuery('#sms ul li').slideDown();
			jQuery('#sms ul li a').each(function(index) {
				jQuery(this).attr('href', response[index].link);
				jQuery(this).html(response[index].title);
			});
		});
	}

