var int = 0;

$.event.special.tripleclick = {

    setup: function(data, namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.bind('click', jQuery.event.special.tripleclick.handler);
    },

    teardown: function(namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.unbind('click', jQuery.event.special.tripleclick.handler)
    },

    handler: function(event) {
        var elem = this, $elem = jQuery(elem), clicks = $elem.data('clicks') || 0;
        clicks += 1;
        if ( clicks === 3 ) {
            clicks = 0;

            // set event type to "tripleclick"
            event.type = "tripleclick";

            // let jQuery handle the triggering of "tripleclick" event handlers
            jQuery.event.handle.apply(this, arguments)
        }
        $elem.data('clicks', clicks);
    }

};

$(document).ready(function(){
	
  	

	$('#menu .wrap, #videos li').mouseover(function(){
		$(this).addClass('hover');
	})
	.mouseout(function(){
		$(this).removeClass('hover');
	});
	
	$('#menu .submenu').mouseover(function(){
		$(this).parent().mouseover();
	})
	.mouseout(function(){
		$(this).parent().mouseout();
	});
	
	$('#menu .wrap').click(function(){
		//alert($(this).children('h3').has('a').html());
  		if ($(this).children('h3').has('a').html())	
			window.location = $('h3 > a',this).attr('href');
	});
	
	$('#gal-menu li a').click(function(){
		var gal_id = $(this).attr('rel');
		$('#view img').hide().removeClass('active');
		$('#videos li').removeClass('active');
		$('#video-view > div').hide();
		
		$('.content').empty();
		$('#' + gal_id + '-text').clone().appendTo($('.content')).show();
		
		$('#' + gal_id + ' img:first').addClass('active').fadeIn(500, function(){
			$('#view > img, #view > object').remove();	
		});

		
		clearInterval(int);
		int = setInterval(function() { gallery_go(gal_id); }, 3000);
		return false;
	});
	
	$('a[rel="video"]').click(function(){
		if (!$(this).closest('li').hasClass('empty')) {
	  		clearInterval(int);
	  		var video_id = $(this).attr('href');
	  		$('#view > img, #view > object').remove();
	  		$('#video-view > div').hide();
	  		$(video_id).show();
			$('#videos li').removeClass('active');
			$(this).parent().addClass('active');
		}
		return false;
	});
	
	$('#videos li').click(function(){
		$(this).find('a').click();
		return false;
	});
	
	$('a[rel="blank"]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	$('a[href^="http://"]:not(a[rel="blank"])').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	/*$("a[href='http://www.edinros.ru/']").unbind('click').click(function(){
		//window.open($(this).attr('href'));
		//return false;
	}).bind("tripleclick", function() {
	  $(this).html('<img src="../images/logos/ebanaya-rossiya.png" alt="Ебаная Россия" />');
	  $(this).attr('href','http://rospil.info/');
	  return false;
	});*/

});

function gallery_go(gal_id) {

	var index = $('#' + gal_id + ' img').index($('#' + gal_id + ' img.active'));

	if (index+1 == $('#' + gal_id + ' img').size()) {
		$('#' + gal_id + ' img.active').removeClass('active');
		$('#' + gal_id + ' img:first').addClass('active').fadeIn(500, function(){
			$('#' + gal_id + ' img:last').hide();
		});
	} else {
		$('#' + gal_id + ' img.active').removeClass('active').next('img').addClass('active').fadeIn(500, function(){
			$('#' + gal_id + ' img.active').prev('img').hide();
		});

	}

	//$('#' + gal_id + ' img.active')
}

function str_replace(search, replace, subject, count) {
 
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}



