$().ready(function()
{

    $('#login_float').jqm({
        trigger: '#login_trigger',
        overlay: 60,
        overlayClass: 'blackOverlay',
	onHide: function(hash) {
	    $('#login_float div#login_error').hide()
	    hash.o.remove();
	    $('#login_float').hide();
	}
    }).jqmAddTrigger($('#login_comment_trigger')).jqmAddTrigger($('#prijava_trigger')).jqmAddTrigger($('#login')); // kod clanka dodaje trigger :)


	$('#avatar_float').jqm({
		trigger: '#avatar_trigger',
		overlay: 60,
		overlayClass: 'blackOverlay'
	});

	$('#status_float').jqm({
		trigger: '#status_trigger',
		overlay: 60,
		overlayClass: 'blackOverlay'
	});

    $('#comment_float').jqm({
		trigger: '#comment_trigger',
		overlay: 60,
		overlayClass: 'blackOverlay'
    });

	$('#login_float_fb_connect').jqm({
        overlay: 60,
        overlayClass: 'blackOverlay'
	});

    $('#send_content_float').jqm({
		trigger: '#send_content_trigger',
		overlay: 60,
		overlayClass: 'blackOverlay',
		onHide: function(hash) {
		    $('#send_content_float form#frm_send_recommend').show();
		    $('#send_content_float div#send_sucess').hide();
		    $('#send_content_float div#send_error').hide();
		    hash.o.remove();
		    $('#send_content_float').hide();
		}
    }).jqmAddTrigger($('#send_content_trigger_2'));

    $('#ex3b').jqm({
        trigger: '#ex3bTrigger',
        overlay: 50
    });

    // login
    $('#ex3c').jqm({
        trigger: '#ex3cTrigger',
        overlay: 50
    });

    // posalji prijatelju
    $('#ex3d').jqm({
        trigger: '#ex3dTrigger',
        overlay: 50
    });

    $('input.jqmdX')
		.focus(
		    function(){ $(this).addClass('jqmdXFocus'); })
		.blur(
			function(){ $(this).removeClass('jqmdXFocus'); });
	}
);


function do_login() {
    var user  = $('#frm_login input#korisnicko_ime').val();
    var pass  = $('#frm_login input#lozinka').val();
    
    $.ajax({
		type: 'POST',
		url : APP_REWRITE_BASE + 'index.php?cmd=do_login&action=login',
		data : 'korisnicko_ime='+user+'&lozinka='+pass,
		success : function(raw_data) {
	
			var pattern = /^(ERR|OK):(.*?)$/i;
			var m = pattern.exec(raw_data);
			if (m.length == 3) {
				var data = m[1];
				var err  = m[2];
			} else {
				var data = '';
				var err  = raw_data;
			}
			
			if (data == 'OK')
			{
			    window.location.reload();
			} else
			{
			    $('#login_float div#login_error span').html(err);
			    $('#login_float div#login_error').show()
			}
		}
    });
}

function posalji_sadrzaj() {
	$.ajax({
	    type: 'POST',
	    url: APP_REWRITE_BASE + 'index.php?cmd=ajax_posalji_prijatelju&action=posalji',
	    //data: 'content_id='+p_clanak_id+'&content_type=1',
	    data: {
		   'content_id' : $('input#content_id').val(),
		   'content_type' : '1',
		   'source_mail' : $('input#source_mail').val(),
		   'dest_mail' : $('input#dest_mail').val(),
		   'zbroj' : $('input#zbroj').val(),
		   'seckey' : $('input#seckey').val()
		},
	    success: function(data) {
		$('input#zbroj').val('');
		if (data == 'OK:') {
		    $('#send_content_float form#frm_send_recommend').hide();
		    $('input#source_mail').val('');
		    $('input#dest_mail').val('');
		    $('#send_content_float div#send_sucess').show();
		} else {
		    $('#send_content_float div#send_error span').html(data.substring(4));
		    $('#send_content_float div#send_error').show();
		}
	    }
	});
}


var _ppk_in_progress = false;

function komentari_controller_new( p_content_id, p_content_type )
{

    this._get_list_url = APP_REWRITE_BASE + 'index.php?cmd=ajax_content_komentari';
    this._submit_url   = APP_REWRITE_BASE + 'index.php?cmd=ajax_content_komentari&action=komentiraj';
    this._pohvali_url  = APP_REWRITE_BASE + 'index.php?cmd=ajax_content_komentari&action=pohvali';
    this._pokudi_url   = APP_REWRITE_BASE + 'index.php?cmd=ajax_content_komentari&action=pokudi';
    this._komentari_pokazi = APP_REWRITE_BASE + 'komentar/' + p_content_id +'/';

    this._current_page = 1;
    this._current_sort = 'kronoloski';
    this._content_id = p_content_id;
    this._content_type = p_content_type;
    var obj = this;

    this.set_cookie = function( 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" : "" );
    }
    
    this.get_cookie = function( name ) {

        var start = document.cookie.indexOf( name + "=" );
        var len = start + name.length + 1;
        if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )  { return null; }
        if ( start == -1 ) return null;
        
        var end = document.cookie.indexOf( ";", len );
        if ( end == -1 ) end = document.cookie.length;
        return unescape( document.cookie.substring( len, end ) );
    }
    
    this._set_initial_cookie = function (p_komentar_id) {
	var _cookie_name = 'VECERNJIkomentar'+p_komentar_id;
	var _cookie = this.get_cookie(_cookie_name);
	if (_cookie == null) {
	    this.set_cookie(_cookie_name, '0', false, '/');
	}
    }

    this.submit = function()
    {
		$('div#comment_float div#login_error').hide();
		$('div#comment_float div#login_sucess').hide();
		var _tekst = $( '#id_komentar_tekst' ).val();
		var _fb_publish = $( 'input#send_fb_comment').is(':checked') ? 1 : 0;

        if( _tekst != '' ){
            //$.get( _url );
			$.ajax({
				url: this._submit_url,
				type: 'POST',
				data: 'content_id=' + this._content_id + '&content_type=' + this._content_type + '&komentar_tekst=' + encodeURIComponent( _tekst ) +'&fb_comment='+_fb_publish,
				success: function(msg) {
				    if (msg == 'OK:') {
						window.location = obj._komentari_pokazi;
				    } else {
						var error = msg.substr(4);
						$('div#comment_float div#login_error span').html(error);
						$('div#comment_float div#login_error').show();
				    }
				}
			});
		}
    };

    this.pohvali = function( p_komentar_id )
    {
	$( '#span_komentar_ocjena_toolbar_' + p_komentar_id).fadeOut( 50 );

	if (!_ppk_in_progress) {

	    _ppk_in_progress = true;
	    this._set_initial_cookie(p_komentar_id);

	    var _url = this._pohvali_url + '&komentar_id=' + p_komentar_id;

	    $.get( _url, function( data ){
	        
	        if( data.substring(0,3) == 'OK:' ){

	            var _ocjena = parseInt( data.substring( 3, data.length ) );

	            $( '#span_komentar_ukupna_ocjena_' + p_komentar_id).text( _ocjena );
    
	        } else {
	            $( '#span_komentar_ocjena_toolbar_' + p_komentar_id).fadeIn( 50 );
		}
	        window.setTimeout('_ppk_in_progress = false;',50);
	    });
	}
    }

    this.pokudi = function( p_komentar_id )
    {
	$( '#span_komentar_ocjena_toolbar_' + p_komentar_id).fadeOut( 50 );

	if (!_ppk_in_progress) {

	    _ppk_in_progress = true;
	    this._set_initial_cookie(p_komentar_id);

	    var _url = this._pokudi_url + '&komentar_id=' + p_komentar_id;

	    $.get( _url, function( data ){
	        if( data.substring(0,3) == 'OK:' ){

	            var _ocjena = parseInt( data.substring( 3, data.length ) );

	            $( '#span_komentar_ukupna_ocjena_' + p_komentar_id).text( _ocjena );
	            //$( '#span_komentar_ocjena_toolbar_' + p_komentar_id).html('OCJENA:<br /><span class="komentar_ocjena">'+_ocjena+'</span>');
		} else {
	            $( '#span_komentar_ocjena_toolbar_' + p_komentar_id).fadeIn( 50 );
		}
	        window.setTimeout('_ppk_in_progress = false;',50);
	    });
	}
    }


    this.sort = function( p_sort )
    {
        this._current_sort = p_sort;
        this._current_page = 1;

        this.get_list();
    }

}


function nc() {
	this.active_nav    = null;
	this.active_subnav = null;
    this.hide_interval = null;
    
    this.mn_out = function()
    {
		cont = this;
		this.hide_interval = window.setTimeout(function() {
			cont.reset();
		},750);
    }
    
    this.mn_in = function (item)
    {
		clearTimeout(this.hide_interval);
		$('ul#navigation li').removeClass('selected');
		$('ul#navigation li#nav_'+item).addClass('selected');
		$('ul.subnavigation').hide();
		$('ul#subnav_'+item).show();
    }
    
    this.sn_out = function()
    {
		cont = this;
		this.hide_interval = window.setTimeout(function() {
			cont.reset();
		},750);
    }
    
    this.sn_in = function() {
		clearTimeout(this.hide_interval);
    }
	
	this.init = function() {
		this.active_nav = $('ul#navigation li.selected');
		var id = this.active_nav.attr('id');
		if (id){
			this.active_subnav =  id.substring(4);
		} else {
			this.active_subnav = 'profil';
		}
	}
	
	
	this.reset = function() {
		//console.log('RESEEET');
		$('ul#navigation li').removeClass('selected');
		this.active_nav.addClass('selected');
		$('ul.subnavigation').hide();
		$('ul#subnav_'+this.active_subnav).show();
	}
	
	this.init();
}
    

    $(document).ready(function(){

		if( typeof $('#slider_special') != 'undefined' ){
	        $("#slider_special").easySlider({
				auto: true, 
				continuous: true,
				prevText: '',
				nextText:'',
				speed: 500,
				pause: 5000
	        });
		}
		
		if( typeof $('#slider_special_2') != 'undefined' ){
	        $("#slider_special_2").easySlider({
				auto: true, 
				continuous: true,
				prevId: 'prevBtn_2',
				nextId: 'nextBtn_2',
				prevText: '',
				nextText:'',
				speed: 500,
				pause: 5000
	        });
		}

		if( typeof $('#slider_galleries') != 'undefined' ){
			$("#slider_galleries").easySlider({
				auto: false, 
				continuous: false,
				prevId: 'prevBtnGalleries',
				nextId: 'nextBtnGalleries',
				prevText: '',
				nextText:'',
				speed: 1500,
				pause: 3000
			});
		}

	if( typeof $('#breaking_wrap') != 'undefined' ){
		$('#breaking_wrap').easySlider({
			auto: false,
			continuous: false,
			prevId: 'prevBreaking',
			nextId: 'nextBreaking',
			prevText: '',
			nextText:'',
			speed: 700
			//pause: 3000
		});
	}

	$("#slider_blogs").easySlider({
			auto: false, 
			continuous: false,
			prevId: 'prevBtnBlogs',
			nextId: 'nextBtnBlogs',
			orientation: 'vertical',
			vertical: true,
			prevText: '',
			nextText:'',
			speed: 1000,
			pause: 3000
        });
	
	if (document.getElementById('title_main_news')) {
	    var back_main_news = document.getElementById('title_main_news').offsetHeight;
	    back_main_news = back_main_news + "px";
	    document.getElementById('back_main_news').style.height = back_main_news;
	}
	
    });
    
    function showHarmonikaTab(tab) {
		$("ul#najcitanije, ul#najkomentiranije, ul#najnovije").hide();
		$("a#tab_najcitanije_new, a#tab_najkomentiranije_new, a#tab_najnovije_new").removeClass("selected");
		$("ul#"+tab).show();
		$("#tab_"+tab+"_new").addClass("selected");
    }

    function showStockHarmonikaTab(tab) {
		$("div#tecajna, div#burze").hide();
		$("a#tab_tecajna, a#tab_burze").removeClass("selected");
		$("div#"+tab).show();
		$("#tab_"+tab).addClass("selected");
    }

	function showBurzeTab(tab) {
		$("#stock_tables").children().hide();
		$("#stock_titles").children().removeClass("selected");
		$("div#"+tab).show();
		$("#tab_"+tab).addClass("selected");
    }

	function showTvTab(tab) {
		$("#tv_programi").children().hide();
		$("#tv_programi_tabs").children().removeClass("selected");
		$("div#"+tab).show();
		$("#tab_"+tab).addClass("selected");
    }

    function vecernji_fm_popup()
    {
        window.open ("http://media.vecernji-list.hr/vecernji.fm/player.html","mywindow","location=0,status=1,scrollbars=0,width=488,height=190");
    }

function fbs_click()
{
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function GetThis(T, C, U, L)
{
	var targetUrl = 'http://www.myspace.com/Modules/PostTo/Pages/?' + 't=' + encodeURIComponent(T) + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
	window.open(targetUrl);
}

function CroportalAdd(URL, TITLE, ABSTRACT, CATEGORY)
{
	var targetUrl = 'http://www.croportal.net/submit?url=' + encodeURIComponent(URL) + '&title=' + encodeURIComponent(TITLE) + '&abstract=' + encodeURIComponent(ABSTRACT) + '&category=' + encodeURIComponent(CATEGORY);
	window.open(targetUrl);
}

function TwitterAdd(URL)
{
	var targetUrl = 'http://twitter.com/home?status=Trenutno citam ' + encodeURIComponent(URL);
	window.open(targetUrl);
}

function DiggAdd(URL)
{
	var targetUrl = 'http://digg.com/submit?phase=2&url=http://' + encodeURIComponent(URL);
	window.open(targetUrl);
}

function load_multimedia(mmid)
{
    $('#google_map_overlay').hide();
    $('#clanak_mm_box').hide();
    $('#clanak_mm_spinner').show();
    var url = APP_REWRITE_BASE + 'index.php?cmd=ajax_load_multimedia&id='+mmid;
    $('#clanak_mm_box').html('');
    $.get(url, null, function(data){
        //alert(data);
        $('#clanak_mm_box').html(data);
        if (($('#clanak_mm_box img').length == 0) || ($.browser.opera)){
            $('#clanak_mm_spinner').hide();
            $('#clanak_mm_box').show();
        } else {
            $('#clanak_mm_box img').bind('load',function(){
                $('#clanak_mm_spinner').hide();
                $('#clanak_mm_box').show();
            });
        }
    });
}

function show_map_div() {
    $('#clanak_mm_spinner').hide();
    $('#clanak_mm_box').hide();
    $('#google_map_overlay').show();
    if (!gmap_controller._map_loaded) {
	gmap_controller.init_map();
    }
/*
    map.checkResize();
    map.setCenter(point,5);
*/
}


$().ready(function(){
        $("#clanak_m-slika_box").easySlider({
			auto: false, 
			continuous: false,
			prevId: 'prevBtnClanakGallery',
			nextId: 'nextBtnClanakGallery',
			prevText: '',
			nextText:'',
			speed: 700,
			pause: 3000
        });
        $('a[rel=lightbox]').fancybox({
           'overlayOpacity' : 0.6,
           'overlayColor'   : '#000000',
           'opacity'        : false,
           'centerOnScroll' : true,
           // inside, outside, over
           'titlePosition'  : 'over'
        }
        );
});

function show_full_image(p_id){
    $('a#lb_'+p_id).click();
}


function ocjena_controller( p_content_id )
{

    this._ocjeni_url = APP_REWRITE_BASE + 'index.php?cmd=ajax_clanak_ocjeni&action=ocjeni';

    this._content_id = p_content_id;

    this.ocjeni = function( p_ocjena)
    {
        var _url = this._ocjeni_url + '&content_id=' + this._content_id + '&ocjena=' + p_ocjena;

        $( '#clanak_ocjena').hide();
        $( '#clanak_ocjena_spinner').show();
        
        $( '#clanak_ocjena').load( _url , function(data)
        {
            $( '#clanak_ocjena_spinner').hide();
            $( '#clanak_ocjena').show();
        });
    }
}



