var prevTitle = null;
jQuery(document).ready(function () {
    jQuery('#newsCycle').cycle({
        timeout: 6500,
        speed:   600,
        startingSlide: 0,
        //fx: 'turnLeft',
        before: function() {
            //added selected class to navigation li on change
            var title = this.lang;
            jQuery("li[lang="+title+"]").addClass("selected");
            jQuery("li[lang="+prevTitle+"]").removeClass("selected");
            prevTitle = title;
        }
    });

    jQuery('#homePlayerCycle').cycle({
        timeout: 5000,
        speed:   600,
        fx: 'scrollHorz',
        random: 1,
		pause:   1,
        next:   '#next',
        prev:   '#prev'
		
    });
	
	jQuery("#explore").click(function (){
		if (jQuery("#exploreDiv").css("display") == "block"){
			jQuery("#exploreDiv").fadeOut(500);									
		}else{
			jQuery("#exploreDiv").fadeIn(500);									
		}							   
		return false;
	});	

    jQuery(function() {
        jQuery('#prizoriscaTabs').tabs({
            fxFade: true,
            fxSpeed: 'normal'
        });
        jQuery('#razporediTabs').tabs({
            fxFade: true,
            fxSpeed: 'normal'
        });
    });


    jQuery('#goto1').hover(function() {
        jQuery('#newsCycle').cycle(0);
        return false;
    });
    jQuery('#goto2').hover(function() {
        jQuery('#newsCycle').cycle(1);
        return false;
    });
    jQuery('#goto3').hover(function() {
        jQuery('#newsCycle').cycle(2);
        return false;
    });
    jQuery('#goto4').hover(function() {
        jQuery('#newsCycle').cycle(3);
        return false;
    });
	
	// hide banner if smaller content than 1080px
	
	jQuery(function(){
		var contentHeight = 1080; 
		var defHeight = jQuery("#contentW").height();
		if(defHeight < contentHeight){
			jQuery("#bigTrigiBanner").css('display','none');
		}
	});

	
	jQuery('table.nasprotniki tr:odd').addClass('odd');
	jQuery('table.nasprotniki tr:even').addClass('even');
});

jQuery(document).ready(function(){
    //select all links in ul with rel = lightbox
    jQuery("a[rel='lightbox']").nyroModal({
        contentError:'',
        type:'image',
        errorClass:'',
        resizeable:false,
        debug:false,
        css:{
            content:{
                overflow: 'hidden'
            }
        },
        minWidth: 1,
        minHeight: 1,
        gallery:'lightbox'
    });

    //here you can alter data before output!
    jQuery.fn.nyroModal.settings.endFillContent = function(elts, settings) {
        if (elts.content[0].lastChild.nodeName == 'DIV'){
            var descDiv = elts.content[0].lastChild;
            descDiv.className = "lightboxPicDescription";
        }
/*
        if (typeof elts.contentWrapper[0].children[1].children[0] != 'undefined' && elts.contentWrapper[0].children[1].children[0].nodeName == 'DIV'){
            var descDiv = elts.contentWrapper[0].children[1].children[0];
            descDiv.style.overflow = "hidden";
        }
*/
    }

    jQuery('a.nelson').bind('click', function() {
        setTimeout ( "animate('div.nelson', '209px', '319px', 500)", 50);
        return false;
    });

    jQuery('div.nelson').bind('mouseleave', function() {
        jQuery('div.nelson').animate({
            height: '0px',
            top: '528px'
        }, 500, function() {
        });
    });

    // cape
    jQuery('a.cape').bind('click', function() {
        setTimeout ( "animate('div.cape', '209px', '326px', 500)", 50);
        return false;
    });

    jQuery('div.cape').bind('mouseleave', function() {
        jQuery('div.cape').animate({
            height: '0px',
            top: '535px'
        }, 500, function() {
        });
    });

    // durban
    jQuery('a.durban').bind('click', function() {
        setTimeout ( "animate('div.durban', '209px', '151px', 500)", 50);
        return false;
    });
    jQuery('div.durban').bind('mouseleave', function() {
        jQuery('div.durban').animate({
            height: '0px',
            top: '360px'
        }, 500, function() {
        });
    });

    // mangaung
    jQuery('a.mangaung').bind('click', function() {
        setTimeout ( "animate('div.mangaung', '209px', '121px', 500)", 50);
        return false;
    });
    jQuery('div.mangaung').bind('mouseleave', function() {
        jQuery('div.mangaung').animate({
            height: '0px',
            top: '330px'
        }, 500, function() {
        });
    });

    // johan
    jQuery('a.johannesburg').bind('click', function() {
        setTimeout ( "animate('div.johan', '209px', '32px', 500)", 50);
        return false;
    });
    jQuery('div.johan').bind('mouseleave', function() {
        jQuery('div.johan').animate({
            height: '0px',
            top: '241px'
        }, 500, function() {
        });
    });


    // nelspuit
    jQuery('a.nelspuit').bind('click', function() {
        setTimeout ( "animate('div.nelspuit', '209px', '-40px', 500)", 50);
        return false;
    });
    jQuery('div.nelspuit').bind('mouseleave', function() {
        jQuery('div.nelspuit').animate({
            height: '0px',
            top: '169px'
        }, 500, function() {
        });
    });

    // tshwane
    jQuery('a.tshwane').bind('click', function() {
        setTimeout ( "animate('div.tshwane', '209px', '-15px', 500)", 50);
        return false;
    });
    jQuery('div.tshwane').bind('mouseleave', function() {
        jQuery('div.tshwane').animate({
            height: '0px',
            top: '194px'
        }, 500, function() {
        });
    });

    // rustenburg
    jQuery('a.rustenburg').bind('click', function() {
        setTimeout ( "animate('div.rustenburg', '209px', '-55px', 500)", 50);
        return false;
    });
    jQuery('div.rustenburg').bind('mouseleave', function() {
        jQuery('div.rustenburg').animate({
            height: '0px',
            top: '154px'
        }, 500, function() {
        });
    });

    // polokwane
    jQuery('a.polokwane').bind('click', function() {
        setTimeout ( "animate('div.polokwane', '209px', '-95px', 500)", 50);
        return false;
    });
    jQuery('div.polokwane').bind('mouseleave', function() {
        jQuery('div.polokwane').animate({
            height: '0px',
            top: '112px'
        }, 500, function() {
        });
    });
    jQuery('#contentMain').mouseenter(function() {
        jQuery('div.johan').animate({
            height: '0px',
            top: '241px'
        }, 500, function() {
        });
        jQuery('div.mangaung').animate({
            height: '0px',
            top: '330px'
        }, 500, function() {
        });
        jQuery('div.nelson').animate({
            height: '0px',
            top: '528px'
        }, 500, function() {
        });
        jQuery('div.polokwane').animate({
            height: '0px',
            top: '112px'
        }, 500, function() {
        });
        jQuery('div.rustenburg').animate({
            height: '0px',
            top: '154px'
        }, 500, function() {
        });
        jQuery('div.tshwane').animate({
            height: '0px',
            top: '194px'
        }, 500, function() {
        });
        jQuery('div.nelspuit').animate({
            height: '0px',
            top: '169px'
        }, 500, function() {
        });
        jQuery('div.durban').animate({
            height: '0px',
            top: '360px'
        }, 500, function() {
        });
        jQuery('div.cape').animate({
            height: '0px',
            top: '535px'
        }, 500, function() {
        });
    });
	
	//user contribution form show
	
	jQuery('.sendContribution').click(function (){
		jQuery('#user_contributions').slideDown();
		jQuery(this).slideUp();
		return false;
	});

    if(jQuery("#igra").length > 0) {
        loadFlash();
    }
	 if(jQuery("#supradynFlash").length > 0) {
        loadFlashSupradyn();
    }
	if (jQuery('#trigishop').get(0) != undefined) {
        loadFlashTrigiShop();
    }
	if (jQuery('#uzivajmoSponzorjeBanner').get(0) != undefined) {
        loadFlashUSP();
    }
	if (jQuery('#bigTrigiBanner').get(0) != undefined) {
        loadFlashBigTrigi();
    }

});


// Animate
function animate (className, height, top, time) {
    jQuery(className).animate({
        height: height,
        top: top
    }, time, function() {});

    return false;
}

//User contributions
jQuery(document).ready(function(){
    jQuery("input[name='sp2010_user_contrib_type']").bind('click', function() {
        if (this.value == "image"){
            jQuery('.sp2010_user_contrib_image').removeClass('hidden');
            jQuery('.sp2010_user_contrib_image_comment').removeClass('hidden');
            jQuery('.sp2010_user_contrib_video').addClass('hidden');
            jQuery('.sp2010_user_contrib_video_comment').addClass('hidden');
        }else{
            jQuery('.sp2010_user_contrib_image').addClass('hidden');
            jQuery('.sp2010_user_contrib_image_comment').addClass('hidden');
            jQuery('.sp2010_user_contrib_video').removeClass('hidden');
            jQuery('.sp2010_user_contrib_video_comment').removeClass('hidden');
        }
    });
});

// This is used at news page to show videos after we click play
jQuery(document).ready(function(){
    jQuery("a[rel='videoNews']").bind('click', function() {
        // Get news ID which we need for specific operations on our news structure
        var id = this+'';
        id=id.split('#')[1];

        // Hide main textual news
        jQuery('#newsText'+id).fadeOut("slow");
        jQuery('#newsText'+id).addClass('hidden');

        // Show video news
        //jQuery('#videoNews'+id).css("display", "block");
        jQuery('#videoNews'+id).fadeIn("normal");
        jQuery('#videoNews'+id).removeClass('hidden');

        // Get video, pic params
        var videoPic = jQuery('#videoPic'+id).val();
        var videoClip = jQuery('#videoClip'+id).val();
        var videoRatio = jQuery('#videoRatio'+id).val();

       var videoSmallRatio = jQuery('#smallvideo'+id).val();

        if (videoSmallRatio == 1){
            //400 x 224 - 16:9
            if (videoRatio == 'wide'){
                var width = 400;
                var height = 224;
            }else{
                //340 x 256 - 4:3
                var width = 340;
                var height = 256;
            }
        }else{
             if (videoRatio == 'wide'){
                var width = 640;
                var height = 360;
            }else{
                //340 x 256 - 4:3
                var width = 640;
                var height = 480;
            }
        }

        // Engage flash
        loadVideoPlayer('newsVideoFlash'+id, videoClip, videoPic, width, height);

        // Dont use link
        return false;
    });
});

// LIKE IT CODE
jQuery(document).ready(function(){    
    jQuery("#rateDown").bind("click", function(e){
        jQuery.get("index.php?r=likeItVote", {url: jQuery(this).attr('href'), type: 'minus'}, function(data){
            jQuery('.rateDownCount').html(data);
        });

        jQuery("#rateUpLi").toggleClass("hidden");
        jQuery("#rateDownLi").toggleClass("hidden");

        jQuery("#rateUpNoClickLi").removeClass("hidden");
        jQuery("#rateDownNoClickLi").removeClass("hidden");
        
        return false;
    });
    
    jQuery("#rateUp").bind("click", function(e){
        jQuery.get("index.php?r=likeItVote", {url: jQuery(this).attr('href'), type: 'plus'}, function(data){
            jQuery('.rateUpCount').html(data);
        });

        jQuery("#rateUpLi").toggleClass("hidden");
        jQuery("#rateDownLi").toggleClass("hidden");

        jQuery("#rateUpNoClickLi").removeClass("hidden");
        jQuery("#rateDownNoClickLi").removeClass("hidden");
        
        return false;
    });
});

// user contributions
jQuery(document).ready(function(){
    jQuery(".rateDown").bind("click", function(e){
        var rel = jQuery(this).attr('rel');
        var spanClass = ".rateDownCount" + rel;
        jQuery.get("index.php?r=userContributionsLikeItVote", {vote: rel, type: 'minus'}, function(data){
            jQuery(spanClass).html(data);
        });
        return false;
    });

    jQuery(".rateUp").bind("click", function(e){
        var rel = jQuery(this).attr('rel');
        var spanClass = ".rateUpCount" + rel;
        jQuery.get("index.php?r=userContributionsLikeItVote", {vote: rel, type: 'plus'}, function(data){
            jQuery(spanClass).html(data);
        });
        return false;
    });
});

// LIKE IT CODE

//this is used for all fake video players
jQuery(document).ready(function(){
    jQuery("a[rel='video']").bind('click', function() {
        var key = this.name;

        // Get video, pic params
        var videoPic = jQuery('#videoPic'+key).val();
        var videoClip = jQuery('#videoClip'+key).val();
        var videoRatio = jQuery('#videoRatio'+key).val();

        var videoSmallRatio = jQuery('#smallvideo'+key).val();

        if (videoSmallRatio == 1){
            //400 x 224 - 16:9
            if (videoRatio == 'wide'){
                var definedWidth = 400;
                var definedHeight = 224;
            }else{
                //340 x 256 - 4:3
                var definedWidth = 340;
                var definedHeight = 256;
            }
        }else{
             if (videoRatio == 'wide'){
                var definedWidth = 640;
                var definedHeight = 360;
            }else{
                //340 x 256 - 4:3
                var definedWidth = 640;
                var definedHeight = 480;
            }
        }
        //clones player div for appending to body, becouse modalManual deletes the div
        var playerDiv = jQuery("#videoplayer").clone();

        //definedWidth=definedWidth+500;
        //definedHeight=definedHeight+500;
        // Engade flash
        loadVideoPlayer('videoplayer', videoClip, videoPic, definedWidth, definedHeight);

        //jQuery(this).nyroModal({minWidth: definedWidth, minHeight: definedHeight, autoSizable: false, galleryLinks: '',width: definedWidth,height: definedHeight});
        jQuery.nyroModalManual({content: jQuery("#videoplayer"),minWidth: definedWidth, minHeight: definedHeight, autoSizable: false, galleryLinks: '',width: definedWidth, height: definedHeight});

        //append it now - when original is deleted
        jQuery("#body").append(playerDiv);
        // Dont use link
        return false;
    });


    //send bad comments via ajax
    jQuery(".prijavi").click(function (){
        var contribId = this.getAttribute('rel');
        jQuery.ajax({
            type: 'POST',
            url: "index.php?r=SendBadContent&sp2010_user_contrib_id=" + contribId,
            success: function(){
                jQuery.nyroModalManual({
                    //contentError:'',
                    type:'content',
                    content:jQuery("#badContentPopup").html(),
                    errorClass:'',
                    resizeable:true,
                    closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close">Zapri</a>',
                    debug:false,
                    minWidth: 1,
                    minHeight: 1
                });
            }
        });
        return false;
    });
    
});

function loadVideoPlayer(id, video_clip, video_pic, width,height) {
    var video = new SWFObject('resources/files/player.swf','ply',width,height,'8','#ffffff');
    video.addParam('allowfullscreen','true');
    video.addParam('allowscriptaccess','always');
    video.addParam('wmode','opaque');
    video.addParam('flashvars','file=../../'+video_clip+'&image='+video_pic+'&skin=resources/files/nacht-Source.swf');
    video.write(id);
}

function loadFlash(uri) {
     var so = new SWFObject("resources/files/game.swf?ver=1.4", "Igra", "640", "520", "8");
     so.addParam("wmode", "transparent");
     so.write("igra");	
}

function loadFlashSupradyn(uri) {
     var so = new SWFObject("resources/files/200x40_Supradyn_R1_Scene 1.swf", "Supradyn", "217", "43", "8");
     so.addParam("wmode", "transparent");
     so.write("supradynFlash");	
}

function loadFlashUSP() {
    var so = new SWFObject("resources/files/uzivajmo_sponzorje.swf", "Uzivajmo sponzorje", "226", "113", "8");
    so.addParam("wmode", "transparent");
    so.write("uzivajmoSponzorjeBanner");
}

function loadFlashTrigiShop() {
    var so = new SWFObject("http://www.nzs.si/resources/files/trigi-shop.swf", "Trigi shop", "275", "210", "8");
    so.addParam("wmode", "transparent");
    so.write("trigishop");
}
function loadFlashBigTrigi() {
    var so = new SWFObject("http://www.nzs.si/resources/files/big-trigi.swf", "Trigi shop", "248", "544", "8");
    so.addParam("wmode", "transparent");
    so.write("bigTrigiBanner");
}

//textarea limit
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
