$(document).ready(function(){
    daAuth.initSettings();
    var dteNow = new Date();
    var intYear = dteNow.getFullYear();
    $('#copyright').html('Copyright &copy; 2010—'+intYear+' ivi media');
	$('ul#nav-top li').hover(function()
		   { $(this).find('div.hid-nav').stop(true, true).show();

			 $(this).addClass('selected');
			 if($(this).hasClass('selected'))
			 {
				$(this).find('a').addClass('selected');
			 }
			$('li.selected').prev().addClass('no-bg');
			//if($(this).find('a').hasClass('music')){//new design
			//	$('div.hid-nav').hide();
			//	$(this).prev().removeClass('no-bg');
			// }
		   },
		   function() {
			 $(this).removeClass('selected');
			 $(this).find('div.hid-nav').hide();
			 $(this).find('a').removeClass('selected');
			 $('li').removeClass('no-bg');
		   }
	 );
	$('.prize-winner').mouseenter(function() {
		$('.prize-winner-hint').css('display', 'block');
		return false;
	}).mouseleave(function(){
		$('.prize-winner-hint').hide();
    });
    
	
	 
	$('.preview .l').mouseenter(function() {
		$('.preview .l').find('.border-new').hide().animate().stop();
		//$('.preview .l').find('.border-new');
		$(this).find('.border-new').animate({'width': '166px', 'height': '121px', 'left': '0', 'top': '0'}, 200);
		$(this).find('.play').fadeIn('slow');
		return false;
	}).mouseleave(function(){
		$('.border-new').animate({'width': '230px', 'height': '185px', 'left': '-32px', 'top': '-32px'}, 200)
                        .stop(true, true)
                        .hide();
		$('.play').hide();
		$('.explanation').stop(true, true)
                         .hide();
		
    });
	

	$('.col-grid-775 .clearfix').hover(function() {
			$(this).find('.preview .l a span.play').stop(true, true).fadeIn('slow');
		},
		function(){
			$(this).find('.preview .l a span.play').hide();
	});
	 

	$('#cont-1').customScroller();
	$('#cont-2').customScroller();
	$('#cont-3').customScroller();
	$('#cont-4').customScroller();
	$('.control-panel h4').click(function(){
	        //$('.control-panel h4').removeClass('selected');
			$(this).addClass('selected');
			
			$('h4').parent().removeClass('slc');
			$(this).parent().addClass('slc');
			$(this).parent().find('div.test').addClass('visible');


			$('h4').parent().next().removeClass('mar-bot');
            $(this).parent().next().addClass('mar-bot');
	});
	
	$('.test ul li').click(function(){
			var change_text = $(this).html();
			$(this).parents('div.slc').find('h4').html(change_text);
			return false;
			
	});
	
	$('.explanation').mouseenter(function(){
		$(this).hide();
	}).mouseleave(function(){
		$(this).hide();
	}).mouseenter(function(){
        return false;
    }).mousemove(function(e){
      var pageCoord_x = e.pageX;
      var clientCoord_x = e.clientX;
	  if(pageCoord_x >= clientCoord_x)
      {
	    $(this).hide();
	  }
    });

	$('#cusel-scroll-select_year_from').click(function(){
        $('#cuselText').trigger('click');
        return false;
    });

	$('#cusel-scroll-select_year_to').click(function(){
        $('#cusel-scroll-select_year_to').trigger('click');
        return false;
    });


    $('#filter_button_none').click(function(){
        req = false;

        year = null;
        country = null;

        //+
        if($('#filter_type').val())
            type = $('#filter_type').val();
        else
            type = 'all';

        if($('#filter_category').val())
            category = $('#filter_category').val();
        else
            category = 'all';

        if($('#filter_genre').val())
            genre = $('#filter_genre').val();
        else
            genre = 'all';

        //+
        if($('#filter_sort').val())
            sort = $('#filter_sort').val();
        else
            sort = 'by_all';

        if($('#filter_year').val())
            year = $('#filter_year').val();

        //+
        if($('#filter_country').val())
            country = $('#filter_country').val();

        url = '/videos/'+type+'/'+category+'/'+genre+'/'+sort;

        if(year){
            req = true;
            url = url + '/?year_id='+year;
        }

        if(country){
            if(req){
                url = url + '&country_id='+country;
            }
            else{
                url = url + '/?country_id='+country;
            }
        }

        window.location = url;

        return false;
    });

		
    $('#user-reg-form').jqm();
    $('#user-auth-form').jqm();
    
    //$('.show-password-wrapper #ch1').change(function(){
    $('.show-password-wrapper').click(function(){
        showHidePassword('elem-password', $('#elem-save_password').attr('checked'));
    });
		
});

function redirect_years(url){
	if ($('.cuselText').text() != 0)
    {
        var from = $('#from .cuselText').text();
        if(url.indexOf("?") != -1)
            start = '&';
        else
            start = '?';

        url += start + 'year_from='+from;
    }
	if ($('.cuselText').text() != 0)
    {
	    var to = $('#to .cuselText').text();
        if(url.indexOf("?") != -1)
            start = '&';
        else
            start = '?';
        url += start + 'year_to='+to;
    }
    document.location = url;
    return false;
};

function draw_close(obj) {
    var caption = $('h4');
    var link_ul = $(obj).html();
    $(caption).html(link_ul);
}


function filter_add(obj, title, type) {
    if(type == 'all')
        $('#'+title).val('');
    else
        $('#'+title).val(type);
    draw_close(obj);
    return false;
}

function showExplanationPopup(obj, id, type, e) {
    var popup = $('.explanation');
    var x_0 = $(obj).offset().left;
    var y_0= $(obj).offset().top;
    var w = $(window).width();
    var dif = w - x_0;
    var x = x_0 + 32;
    var y = y_0 - 24;

    if(dif <= 388){
            var x = x_0 - 538;
            var y = y_0 - 24;
    }
    if(!type)	type = 1;

    var param = {
        id: id,
        type: type
    };

    $.ajax({
            dataType: 'json',
            type: "POST",
            url: '/video/ajax/entity/information/',
            data: param,
            success: function(res){
                    $('.explanation').hide().css({'left': x, 'top': y}).html(res.html).show();
            }
    });
}

function setCookie(c_name, value, exdays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value + "; path=/";
}

function openUserModal(key) {
    $('.jqmWindow').jqmHide();
    $('#user-' + key + '-form').jqmShow();
    return false;
}
function reloadCaptcha() {
    d = new Date();
    $('#captcha').attr('src', '/user/draw/captcha13' + '?' + d.getTime());
    $('#captcha_value').val('');
}
function showReg() {
    openUserModal('reg');
    reloadCaptcha();
}

function showHidePassword(id, action) {
	
    ec = $('#' + id);
    
    if (ec.length > 0) {
        
        elem = ec.first();    
        //action = $(elem).css('display') !== 'none'; // true is for show, false is for hide
        
        flag = false; // indicates if text element is just created
        ec_text = $('#' + id + '_text');
        
        if (ec_text.length === 0) {
            elem_text = document.createElement('input');
            $(elem_text).attr('id', id + '_text')
                        .attr('type', 'text')
                        .attr('tabindex', $(elem).attr('tabindex'))
                        .addClass($(elem).attr('class'));
            $(elem).css('display', 'none')
                    .attr('disabled', 'disabled');
            $(elem_text).insertAfter(elem);
            flag = true; // text element just created
        }
        else {
            elem_text = ec_text.first();                
        }
            
        
        // showing password
        if (action) {
            $(elem_text).val($(elem).val());            
            $(elem_text).css('display', '')
                        .removeAttr('disabled')
                        .attr('name', $(elem).attr('name'));
            $(elem).css('display', 'none')
                   .removeAttr('name')
                   .attr('disabled', 'disabled');
        }
        // hiding password
        else {
            if (!flag) { // regetting value only if element wasn't just created
                $(elem).val($(elem_text).val());
            }
            $(elem).attr('disabled', 'disabled')
                   .removeAttr('disabled')
                   .css('display', '')
                   .attr('name', $(elem_text).attr('name'));
            
            $(elem_text).css('display', 'none')
                        .removeAttr('name')
                        .attr('disabled', 'disabled');
        }
        
    }
    
}
function redrawSocial() {
    
}
function mergeAccount(b){
    $.ajax({
        url:"/user/ajax/merge",
        dataType:"json",
        type: "POST",
        data: {hash:b},
        success:function(a) {
            window.location.hash='#socnet';
            window.location.reload();
        }
    });
}
function separateAccount(id){
    $.ajax({
        url:"/user/ajax/separate",
        dataType:"json",
        type: "POST",
        data: {hash:id},
        success:function(a) {
            window.location.hash='#socnet';
            window.location.reload();
        }
    });
}
