pageCallBack = function() { 
    ; /* paging breaks tooltip and fancybox on pages, so those pages need to overwrite this method to re-trigger them */
    //keep this at the top or ie6 croaks
} 

function addFavorite(link, image, section, replace) {
    
    $.get("/favorites/add/", {
        "link": link,
        "image": image,
        "section": section
    },
    function(data, status) {
        if (replace && replace==true) {
            var $link = $("#add-to-favorites-link");
            if (status == "success") {
                $link.unbind("click");
                $link.removeAttr("href");
            }
            $link.text(data);
        }
        response = data;
    });
    
}

function openWin(url, height, width, windowname) {
    if(windowname == undefined) {
        windowname = "liveshow";
    } 
	lwin = window.open(url, windowname,"location=0,status=0,scrollbars=0,width=" + width + ",height=" + height);
}

var lastHighlight = '';
$(document).ready(function() {

    /* external links for html 4.01 */
    $('a[rel=external]').attr('target', '_blank');
    
    /* bind a click event to all pager_link classed a tags
    we also add items to the bbq pushState based on the
    pager_link tags already defined  */
    
    $("a.pager_link").live("click", function(e) {
    
        var href = $(this).attr("href"); 
        
        var items = $.deparam.querystring(href);
        $.bbq.pushState({"p": items.page, "s": items.sortby, "o": items.orderby}, 2);
        return false;
        
    });
    
    // This is what handles the hashchange and triggers
    // an event, so that the back buttons work etc.
    $(window).bind("hashchange", function(e) {
        
        //var url = $.bbq.getState("url");
        var hash = location.hash;
        
        if(hash) {
            // do hash stuff
            var test = $.deparam.fragment();
            var page = test.p;
            var sort = test.s;
            var order = test.o;
            var mpa3 = $.deparam.querystring().mpa3;
            
            var newObj = Object();
            newObj.sortby = sort;
            newObj.page = page;
            newObj.orderby = order;
            //newObj.partial = 1;
            if(mpa3 != "") {
                newObj.mpa3 = mpa3;
            }

            var new_url = $.param.querystring(window.location.pathname, newObj) + " #listing";
            $("#listing").load(new_url, '', function() {
                pageCallBack();
            });
        } 

    });
    
    // DO NOT REMOVE THIS
    $(window).trigger("hashchange");
    
    
    /* redirect the selects */
    $("select.redirect").change(function() {
        window.location = $(this).val();
    });
    
    $("a.bmp-promo").live("click", function() {
        newwindow = window.open($(this).attr("href"), '', 'height=420,width=700,location=0,status=0,scrollbars=0');
        return false;
    });

    /* when sorting change the order by via js */
    $("a.sort").click(function(event) {
        event.preventDefault();
        
        // grab the info from the sort link and the first pagination link
        // to build the urls with the info we need
        var first_link = $("a.pager_link:first");
        
        // if the pagination isn't there, use a diff link.
        if($(first_link).attr("href") == null) {
            var backup = true;
            first_link = $(this);
        }
        
        var orderby = $.deparam.querystring($(first_link).attr("href")).orderby;
        var sortby = $.deparam.querystring($(this).attr("href")).sortby;

        
        //change highlighting on the sort link
        if ($(this).attr('id')) {
            var sortId = $(this).attr('id');          
        
            if ($("#"+sortId).html().indexOf('highlight') == -1) {
                $("#"+sortId).html('<span class="highlight">'+sortby.substring(0,1).toUpperCase()+sortby.substring(1, sortby.toString().length)+'</span>');
                if (lastHighlight != '') {
                    var re = /<span class="highlight">/i
                    var re2 = /<span class=highlight>/i
                    var re3 = /<\/span>/i
                    $("#"+lastHighlight).html($("#"+lastHighlight).html().replace(re,''));
                    $("#"+lastHighlight).html($("#"+lastHighlight).html().replace(re2,''));
                    $("#"+lastHighlight).html($("#"+lastHighlight).html().replace(re3,''));
                }
                lastHighlight = sortId;
            }
        }
        
        
        // flip/flop the direction
        if(orderby == "desc") {
            orderby = "asc";
        } else if (orderby == undefined) {
            orderby = "desc";
        } else {
            orderby = "desc";
        }
        
        
        // when they click this we should start at page one, so this is the url that we 
        // use to reload the page, thats why we set page=1
        var reload_page = $.param.querystring($(first_link).attr("href"), "orderby=" + orderby + "&page=1&sortby=" + sortby);

        // now we loop through all the a.pager_links and set their urls to the new order/sort by
        // to match the change that was requested.
        if(backup == true) {
            //alert("HERE");
            $("a.sort").each(function() {
                //alert("CURRENT:" + $(this).attr("href"));
               $(this).attr("href", $.param.querystring($(this).attr("href"), "orderby=" + orderby)); 
               //alert("NEW:" + $(this).attr("href"));
            });
        }
        
        $("a.pager_link").each(function() {
            $.param.querystring($(this).attr("href"), "orderby=" + orderby + "&sortby=" + sortby);
        })
    
        // finally we reload the page to the reload_page variable, the pagination
        // handles the rest.
        $("#listing").load(reload_page + " #listing", '', function(res, stat) {             
            pageCallBack();
        });
        
    });
    

    /* navigation for latest videos on index page */
    $("a[class^=latest-nav]").click(function() {
        var $page = parseInt($(this).attr("rel"));
        var $url = $(this).attr("href");
        if($page == 1) {
            $("a.latest-nav-prev").attr("rel", 3);
            $("a.latest-nav-next").attr("rel", 2);
        } else if ($page == 2) {
            $("a.latest-nav-prev").attr("rel", 1);
            $("a.latest-nav-next").attr("rel", 3);
        } else {
            $("a.latest-nav-prev").attr("rel", 2);
            $("a.latest-nav-next").attr("rel", 1);
        }
       $('#listing').load($url + "&page=" + $page + " #listing"); 
       return false;
    });
    
    /* navigation for latest models on index page */
    $("a[class^=latest-model]").live("click", function() {
        
        var $url = $(this).attr("href");
        var $page = parseInt($(this).attr("rel"));
        var $direction = $(this).attr("class").split("latest-model-")[1];
        if($direction == "next") {
            if($page == 3) {
                $new_page = 1;
            } else {
                $new_page = $page + 1;
            }
        } else if($direction == "prev") {
            if($page == 1) {
                $new_page = 3;
            } else {
                $new_page = $page - 1;
            }
        }

        $('#feature-model').load($url + "&model_page=" + $new_page + " #feature-model"); 
        return false;
    });
    
    
    /* Dropdown Menu */
    $("ul.dropdown li").hover(function(){
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    }, function(){
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    });
    
    $("ul.dropdown li ul li:has(ul)").find("a:first").prepend('<img src="/media/images/design/arrow_sub_white.gif" alt="" style="float: right; padding: 0 0 0 0; margin: 0 0 0 0;" />');
   
   
   $("a[rel=myaccount-auth]").click(function(e) {
       e.preventDefault();
       //openWin($(this).attr("href"), 70, 275, "_MyAccount");
       window.open($(this).attr("href"), "_MyAccount");
   });
    
   $("a[rel=live-show]").click(function(e) {
      e.preventDefault();
      openWin($(this).attr("href"), 605, 945, "liveshow"); 
   });
   
   $("a[rel=set-screen-name]").click(function(e) {
       e.preventDefault();
       $("#set-screen-name").toggle();
       $("#set-screen-name2").toggle();
       $("#set-screen-name-input").focus();
   });
   
   $("#set-screen-name-button").click(function(e) {
      e.preventDefault();
      
      var name = $.trim($("#set-screen-name-input").val());
      if (name != "") {
          $.ajax({ 
                  url: '/1on1/set_name', 
                  data: { 
                      'name': name
                  },
                  type: 'POST',
                  dataType: 'html',
                  context: document.body, 
                  success: function(data){
                    var parts = data.split("\:");
                    if (parts[0].toLowerCase() == "success") {
                      $("#set-screen-name2").toggle();
                      $("#screen-name-set").html("&nbsp;" + parts[1]);
                    }
                    else {
                      alert(parts[1]);
                    }  
                  },
                  error: function(){
                      alert("Error");
                  }
          });
      }
        
   });
   
});

toggleDesc = function(id) {
    $('#show_description').toggle();
    ($('#desc_toggle').html().indexOf('Show Full') != -1) ? $('#desc_toggle').html('Hide Full Description <img src="/media/images/design/arrow_help_up.gif" border="0" alt=""/>')
                                                          : $('#desc_toggle').html('Show Full Description <img src="/media/images/design/arrow_help_down.gif" border="0" alt=""/>')
}

