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("#explore").click(function (){
		if (jQuery("#exploreDiv").css("display") == "block"){
			jQuery("#exploreDiv").fadeOut(500);									
		}else{
			jQuery("#exploreDiv").fadeIn(500);									
		}									   
		return false;
	});	

    jQuery('#homePlayerCycle').cycle({
        timeout: 4000,
        speed:   600,
        fx: 'scrollHorz',
        random: 1,
        next:   '#next',
        prev:   '#prev'
    });


    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;
    });
});

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() {
        });
    });

});

// Animate
function animate (className, height, top, time) {
    jQuery(className).animate({
        height: height,
        top: top
    }, time, function() {});

    return false;
}

// 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);
        });

        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);
        });

        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();

        //hardcoded for popup video
        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;
    });
	
	
	jQuery.nyroModalSettings({
		processHandler: function(settings) {

			var from = settings.from;

			var key = from.name;

			// Get video, pic params
			var videoRatio = jQuery('#videoRatio'+key).val();

			var definedWidth = 640;
			var definedHeight = 360;        

			if (videoRatio != null) {
				if (videoRatio == 'wide'){
					var definedWidth = 640;
					var definedHeight = 360;
				}else{
					var definedWidth = 480;
					var definedHeight = 360;
				}
			}

			if (from && from.href && from.href.indexOf('http://www.youtube.com/watch?v=') == 0) {
				from.href += '&autoplay=1&rel=0&showsearch=0&hd=1';
				jQuery.nyroModalSettings({
					type: 'swf',
					height: definedHeight,
					width: definedWidth,
					url: from.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
				});
			}
		}
	});
	
});

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);
}
