$(function() {
function SetCookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function GetCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}				

curpagepoll = GetCookie("kuwaitclub_poll_id");
if (curpagepoll) {
    $.getJSON("/xmlhttp/poll_get/", {'poll_id':curpagepoll}, function(data){
	$('#poll_frame').html(data.content);
   })
}

img1 = new Image();
img1.src='/s/new/images/image-menu/mi-ipod-hover.gif';
img2 = new Image();
img2.src='/s/new/images/image-menu/mi-video-hover.gif';
img3 = new Image();
img3.src='/s/new/images/image-menu/mi-foto-hover.gif';



// function is refactored using JQuery, there is no reason to use separate client-side XMLHTTP framework

$('#poll_form').submit(function() {

	var el = $(this).find('input:checked').get(0);

	if (!el) {

        alert("يرجي الاختيار");

        return false;

    	}

    	

	var choice = el.value;

	var poll_id = $('#poll_id').get(0).value;

	// setting client-side cookie is bullshit, this must be done in response header.

	$.getJSON("/xmlhttp/poll_vote/", {'poll_id':poll_id, 'choice':choice}, function(data) {

		if (data.status != 200)

			return;



		var d = new Date();

		d.setTime(d.getTime()+365*24*3600*1000);

		document.cookie = 'kuwaitclub_poll_id='+poll_id+'; expires='+d.toUTCString() + ";path=/";



		$('#poll_frame').html(data.content);

	});



	return false;	

});

var image_interval1 = 0;
var image_interval2 = 0;
var image_interval3 = 0;

$('.nav a').click(function() {
  
	if ( $(this).hasClass('category') )
		return true;

	var id = this.id.substr(3)
	var link = $(this);
	$.getJSON("/xmlhttp/article/", {'id': id}, function(data) {

		if (data.status != 200)
			return;

		link.parent().parent().parent().children('.ajaxed').eq(0).html(data.content);
		link.parent().children('.active').removeClass('active');
		link.addClass('active');
		var cat_id = link.parent().parent().parent().parent()[0].id.substr(11);
		if (cat_id == 1){
		  clearInterval(image_interval1);
		  image_interval1 = setInterval("$('#article_cat1 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);
	  } else if (cat_id == 2){
	    clearInterval(image_interval2);
		  image_interval2 = setInterval("$('#article_cat2 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);
	  } else if (cat_id == 3){
	    clearInterval(image_interval3);
		  image_interval3 = setInterval("$('#article_cat3 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);
	  }
	});

	return false;	

});

image_interval1 = setInterval("$('#article_cat1 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);
image_interval2 = setInterval("$('#article_cat2 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);
image_interval3 = setInterval("$('#article_cat3 .nav').each(function() { var active = $(this).children('.active').eq(0); var el = !active.next().get(0) ? $(this).children('a').eq(1) : active.next(); el.click(); });", 7000);


$('.main-menu li').hover(function() {
	$(this).children('.submenu').fadeIn('fast');

}, function() {

	$(this).children('.submenu').fadeOut('fast');

});



$('.main-menu .daddy').click(function() {

	return false;

});

$('#video-link').click(function() {
	var win = window.open(this.href,'Video','width=520,height=630');
	win.focus();
	return false;
});

/****************************************makkalot*********************************************************/
function remove_items(object_jq){
    object_jq.children('option').not('.dstatic').remove();
}

//copy option for ie
var $copy_leaugue_options = $('#dleagueoptions').children('option').not('.dstatic').clone();
remove_items($('#dleagueoptions'));

var $copy_cup_options = $('#dcupoptions').children('option').not('.dstatic').clone();
remove_items($('#dcupoptions'));


//copy leagues
var $copy_leagues = $('#dleague').children('option').not('.dstatic').clone();
remove_items($('#dleague'));

//copy cups
var $copy_cups = $('#dcups').children('option').not('.dstatic').clone();
remove_items($('#dcups'));

//set some default values
$('#dleague, #dleagueoptions,#dcupoptions, #dsport, #dseason','#dcups','#dtype').children('.dstatic').attr('selected', 'selected');

$('#dsport, #dseason, #dtype').change(function() {
	var sport = $('#dsport :selected').val();
	var season = $('#dseason :selected').val();
	var tournamet_type = $('#dtype :selected').val();
	

	SetCookie('dsport', sport, 0, '/');
	SetCookie('dseason', season, 0, '/');
	SetCookie('dtype', tournamet_type, 0, '/');
		
    //some more controls here
	remove_items($('#dleague'));
	remove_items($('#dcups'));
    remove_items($('#dleagueoptions'));	
    remove_items($('#dcupoptions'));	

    var leagues = $('#dleague option');
    var cups = $('#dcups option');

	if (!sport || !season || !tournamet_type) {
        //set the default values here
		leagues.filter('.dstatic')[0].selected = 'selected';
		cups.filter('.dstatic')[0].selected = 'selected';
		$('#dleagueoptions option').filter('.dstatic')[0].selected = 'selected';
		$('#dcupoptions option').filter('.dstatic')[0].selected = 'selected';
		return;
	}

	$copy_leagues.each(function(i, el) {
		el = $(el);
		if (el.hasClass('g'+ sport) && el.hasClass('s' + season))
			el.appendTo($('#dleague'));
	});
    
    $copy_cups.each(function(i, el) {
		el = $(el);
		if (el.hasClass('g'+ sport) && el.hasClass('s' + season))
			el.appendTo($('#dcups'));
	});

    //at the end you should hide or show the league or cup
    if(tournamet_type == "league"){
        //hide cup
        $('#dcups').hide()
        //hide cups options
        $('#dcupoptions').hide()
        //show league
        $('#dleague').show('slow')
        //show league opitons
        $('#dleagueoptions').show('slow')
    }
    if(tournamet_type == "cup"){
        //hide league
        $('#dleague').hide()
        //hide league options
        $('#dleagueoptions').hide()
        //show cup
        $('#dcups').show('slow')
        //show cup opitons
        $('#dcupoptions').show('slow')

    }
    
});

$('#dleague').change(function() {
	var league = $('#dleague :selected').val();
	var options = $('#dleagueoptions').children('option');

	SetCookie('dleague', league, 0, '/');
	
	if (!league) {
	    remove_items($('#dleagueoptions'));
		options.filter('.dstatic')[0].selected = 'selected';
		return;
		}
		
    $copy_leaugue_options.each(function(i, el) {
		el = $(el);
		el.appendTo($('#dleagueoptions'));
	});

	
});	

$('#dcups').change(function() {
	var cup = $('#dcups :selected').val();
	var options = $('#dcupoptions').children('option');

	SetCookie('dcups', cup, 0, '/');
	
	if (!cup) {
	    remove_items($('#dcupoptions'));
		options.filter('.dstatic')[0].selected = 'selected';
		return;
		}
		
    $copy_cup_options.each(function(i, el) {
		el = $(el);
		el.appendTo($('#dcupoptions'));
	});

	
});	

$('#dseason').change(function() {
	var league = $('#dleague :selected').val();
	var cup = $('#dcups :selected').val();
	
	var league_options = $('#dleagueoptions').children('option');
	var cup_options = $('#dcupoptions').children('option');

	SetCookie('dleague', league, 0, '/');
	SetCookie('dcups', cup, 0, '/');
	
    var league_exit = false;
    var cup_exit = false;
	if (!league) {
	    remove_items($('#dleagueoptions'));
		league_options.filter('.dstatic')[0].selected = 'selected';
		league_exit = true
		}
	
    if (!cup) {
	    remove_items($('#dcupoptions'));
		cup_options.filter('.dstatic')[0].selected = 'selected';
		cup_exit = true;
		}

    if(cup_exit == true && league_exit == true)
        return;
    
    if(!league_exit){
    $copy_leaugue_options.each(function(i, el) {
		el = $(el);
		el.appendTo($('#dleagueoptions'));
	});
    $('#dleagueoptions option').filter('.dstatic')[0].selected = 'selected';
    }

    if(!cup_exit){
	$copy_cup_options.each(function(i, el) {
		el = $(el);
		el.appendTo($('#dcupoptions'));
	});
    
    $('#dcupoptions option').filter('.dstatic')[0].selected = 'selected';
    }

});

$('#dleagueoptions').change(function() {
	if (this.value) {
		SetCookie('dleagueoptions', this.value, 0, '/')
		if (this.value != '/data/goal_scorers/')
			window.location = this.value + $('#dleague :selected').val() + '/s/' + $('#dseason :selected').val() + '/'
		else
			window.location = this.value + $('#dseason :selected').val() + '/' + $('#dleague :selected').val() + '/'
	}
});

$('#dcupoptions').change(function() {
	if (this.value) {
		SetCookie('dcupoptions', this.value, 0, '/')
		if (this.value != '/data/cup/goal_scorers/')
			window.location = this.value + $('#dcups :selected').val() + '/s/' + $('#dseason :selected').val() + '/'
		else
			window.location = this.value + $('#dseason :selected').val() + '/' + $('#dcups :selected').val() + '/'
	}
});

if ($('#dsport').hasClass('action')) {
    var dsport = GetCookie('dsport');
    var dtype = GetCookie('dtype');
    var dseason = GetCookie('dseason');
    var dleague = GetCookie('dleague');
    var dcups = GetCookie('dcups');
    var dleagueoptions = GetCookie('dleagueoptions');
    var dcupoptions = GetCookie('dcupoptions');
    
    if (dsport) {
    	$('#dsport').val(dsport);
    	$('#dsport').change();
    }
    
    if (dseason) {
    	$('#dseason').val(dseason);
    	$('#dseason').change();
    }
    
    if (dtype) {
    	$('#dtype').val(dtype);
    	$('#dtype').change();
    }

    if (dleague) {
    	$('#dleague').val(dleague);
    	$('#dleague').change();
    	$('#dleagueoptions').val(dleagueoptions);
    }
    if (dcups) {
    	$('#dcups').val(dcups);
    	$('#dcups').change();
    	$('#dcupoptions').val(dcupoptions);
    }

}
$('#sgseason').change(function() {
	window.location = '/data/single_games/' + this.className.substr(4) + '/' + this.value + '/'
});

/*
if (!$.browser.msie)
	setInterval("var el = $('.vb-group.active'); el.removeClass('active').fadeOut('slow', function(){ el = $(this).next()[0] ? $(this).next().eq(0) : $('#vbtop10 :first-child'); el.addClass('active').fadeIn('slow'); });", 10000);
else
	setInterval("var el = $('.vb-group.active'); el.removeClass('active').hide(); el = el.next()[0] ? el.next().eq(0) : $('#vbtop10 :first-child'); el.addClass('active').show();", 10000);
*/
});
