$(document).ready(function(){

    $('.thumb_item a,#list_view a').click(function(){
        var name = $(this).attr('href');


        hash = name.replace('#','');
        //var player = $f($('.top_box:visible iframe')[0]);
        //player.api('unload');
        $('.top_box:visible iframe').remove();
        $('body,html').animate({scrollTop : 0},'slow');

        //alert(name);
        if (!$('#box_'+hash).is('visible')) {
            $('.top_box').hide();
            $('#box_'+hash).show();
           if ($('#frame_'+hash).is('span')) {
                var src = $('#frame_'+hash).attr('src');
                $('#box_'+hash+' .video_frame').append('<iframe src="'+src+'" width="632" height="355" frameborder="0"  webkitAllowFullScreen allowFullScreen></iframe>');
            }
        }

        return false;
    });
    var item_link = window.location.href;
    var hash = item_link.split('#')[1];
    if ($('#box_'+hash).is('div')) {
        if (!$('#box_'+hash).is('visible')) {
            $('.top_box').hide();
            $('#box_'+hash).show();
        }
    }

    function e_friend(e_add,subject1,body)
    {
     //window.open("mailto:"+e_add+"?subject="+subject1+"&body="+body, 'open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0');
     window.location = "mailto:"+e_add+"?subject="+subject1+"&body="+body;
    }

    $(".submitfriend").click(function(){
        var email = $('#friendemail').val();
        var link = 'Check out this link: '+ $('#permalink').val();
       // e_friend(email,encodeURIComponent('Hey check out this link fro teepeegee.com'),encodeURIComponent('Check out this link: '+link));
       linkmail = "mailto:"+email+"?subject=check%20out%20this%20link&body="+encodeURIComponent(link);
       window.location.href=linkmail;
       return false;
    });

    $('#back_to_top').click(function(){
        $('body,html').animate({scrollTop : 0},'slow');
        return false;
    });



    thumb_height = $('#thumbnail_list').height();
    $('#see_more').hide();
    if (thumb_height>580) {
        $('#thumbnail_list').css('height','560px').css('overflow','hidden');
        $('#see_more').show();
    }

    $('#see_more').click(function(){
        $('#thumbnail_list').height(thumb_height);
        $('#see_more').hide();
        $('html, body').animate({
                     scrollTop: 1180
                 },
                 'slow');
        return false;
    });

    $('#grid_view_button').hide();
    $('#list_view_button').click(function(){
        $(this).hide();
        $('#grid_view_button').show();
         $('#list_view').fadeIn(500);
        $('#grid_view').hide();
       $('html, body').animate({
                     scrollTop: 535
                 },
                 0);
    });
    $('#grid_view_button').click(function(){
        $(this).hide();
        $('#list_view_button').show();
        $('#grid_view').fadeIn(500);
        $('#list_view').hide();
        $('html, body').animate({
                     scrollTop: 535
                 },
                 0);
    });

    /*
    if (!hash) {
        setInterval(function(){
            if ($('.top_box').size()>1) {
                var top_box = $('.top_box:visible');
                if (!$(top_box).next('.top_box').is('div')) {
                    $(top_box).hide();
                    $('.top_box:first').fadeIn(500);
                }
                else {
                    $(top_box).hide().next('.top_box').fadeIn(500);
                }
            }
        },5000);
    }
    */

    $('.submit').click(function(){
        $(this).parent('form').submit();
    });

    $('.more_box form input[type=text]')
        .focus(function() {
            if( this.value == this.defaultValue ) {
                this.value = "";
                //$(this).parent('form').find('input[type=submit]').removeAttr('disabled');
            }
        })
        .blur(function() {
            if( !this.value.length ) {
                this.value = this.defaultValue;
                //$(this).parent('form').find('input[type=submit]').attr('disabled','disabled');
            }
        });

    $('.share_link').click(function(){
        $(this).select();
    });

    $('.more_link a').click(function(){
        $(this).parents('.box_description').find('.more_box').fadeIn(500);
        return false;
    });

    $('.close_box').click(function(){
         $(this).parent('.more_box').fadeOut(500);
    });


    $('.share_icons a').popupWindow({
        centerScreen:1,
        height:450,
        width:600
    });

    if ($('#news_list').is('ul')) {
        $('#news_list li').each(function(){
            var left = $(this).find('.left-side');
            var right = $(this).find('.right-side');
            var img = $(left).find('img');
            $(right).append(img);
            var iframe = $(left).find('iframe');
            $(right).append(iframe)
        });
    }
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if (agentID) {

        // do something special
        $('#footer_spacer').hide();
        $('#footer_wrapper').css('position','relative');

        $('#thumbnail_list ul li .dim').show();
        $('.dim').click(function(){
            $(this).prev('a').click();
        });
    } else {
        $('#thumbnail_list ul li').hover(function(){
            $(this).find('.dim').fadeIn();
        },function(){
            $(this).find('.dim').hide();
        });
        $('.dim').click(function(){
            $(this).prev('a').click();
        });
    }

    el = document.getElementById("scroll_wrapper");
    scroller = new jsScroller(el, 189, 161);

});

function check_top() {
    var pos = $('#scroll_box').css('top');
    if (pos=='0px') {
        $('.box_description h2').css('visibility','visible');
        $('#scroll_up').hide();
    } else {
        $('.box_description h2').css('visibility','hidden');
        $('#scroll_up').show();
    }
}

function jsScroller (o, w, h) {
	var self = this;
	var list = o.getElementsByTagName("div");
	for (var i = 0; i < list.length; i++) {
		if (list[i].className.indexOf("Scroller-Container") > -1) {
			o = list[i];
		}
	}

	//Private methods
	this._setPos = function (x, y) {
		if (x < this.viewableWidth - this.totalWidth)
			x = this.viewableWidth - this.totalWidth;
		if (x > 0) x = 0;
		if (y < this.viewableHeight - this.totalHeight)
			y = this.viewableHeight - this.totalHeight;
		if (y > 0) y = 0;
		this._x = x;
		this._y = y;
		with (o.style) {
			left = this._x +"px";
			top  = this._y +"px";
		}
	};

	//Public Methods
	this.reset = function () {
		this.content = o;
		this.totalHeight = o.offsetHeight;
		this.totalWidth	 = o.offsetWidth;
		this._x = 0;
		this._y = 0;
		with (o.style) {
			left = "0px";
			top  = "0px";
		}
	};
	this.scrollBy = function (x, y) {
		this._setPos(this._x + x, this._y + y);
	};
	this.scrollTo = function (x, y) {
		this._setPos(-x, -y);
	};
	this.stopScroll = function () {
		if (this.scrollTimer) window.clearInterval(this.scrollTimer);
	};
	this.startScroll = function (x, y) {
		this.stopScroll();
		this.scrollTimer = window.setInterval(
			function(){ self.scrollBy(x, y); }, 40
		);
	};
	this.swapContent = function (c, w, h) {
		o = c;
		var list = o.getElementsByTagName("div");
		for (var i = 0; i < list.length; i++) {
			if (list[i].className.indexOf("Scroller-Container") > -1) {
				o = list[i];
			}
		}
		if (w) this.viewableWidth  = w;
		if (h) this.viewableHeight = h;
		this.reset();
	};

	//variables
	this.content = o;
	this.viewableWidth  = w;
	this.viewableHeight = h;
	this.totalWidth	 = o.offsetWidth;
	this.totalHeight = o.offsetHeight;
	this.scrollTimer = null;
	this.reset();
};




