var iTV = {
	vars: {
		site_url: "",
		sessionid: "",
		site_path: "",
		site_domain: "",
		current_active: "",
		scrolldelay: "",
        on_slide: ""
	},
	initialize: function() {
		$(document).ready(function() {
			$('#hoverMenu .kanaly_tabs_podmenu').each(function(){
				$('li').mouseover( function() {
					$('ul', this).css('visibility', 'visible');
				});
				$('li').mouseout( function() {
					$('ul', this).css('visibility', 'hidden');
				});
			});
			
			if($(".hover_img")) {
                $('.hover_img:not(.nohover)').live('mouseover mouseout', function(event) {
                    if (event.type == 'mouseover') {
                        $(this).css('opacity',0);
                    } else {
                        $(this).css('opacity',1);
                    }
                });
			}

    		$('#tools_top > li').bind('mouseover', jsddm_open)
    		$('#tools_top > li').bind('mouseout',  jsddm_timer)
    		
			$("#q").autocomplete(base_uri("szukaj/helper.html"), {
				minChars: 0,
				width: 189,
				matchContains: "word",
				autoFill: false,
				selectFirst: false,
				highlight: false,
				formatItem: function(row, i, max) {
					if (row[2]) {
						return '<a href="#" onclick="window.location=\''+row[1]+'\'">'+row[2]+' '+row[3]+'</a>';
					}
					else {
						return '<a href="#" onclick="window.location=\''+row[1]+'\'">'+row[3]+'</a>';
					}
				},
				formatResult: function(row) {
					return row[0];
				}
			});
			
			/*
			* Szukanie kanalow przy zglaszaniu
			*/
			$("#channel_name").autocomplete(base_uri("user/search_channel"), {
				minChars: 0,
				width: 189,
				matchContains: "word",
				autoFill: false,
				selectFirst: false,
				highlight: false,
				formatItem: function(row, i, max) {
					return '<a href="#" onclick="javascript:insertOption(\''+row[2]+' ('+row[3]+')\', \''+row[1]+'\', \'channel_list\'); return false;">'+row[2]+' ('+row[3]+')</a>';
				},
				formatResult: function(row) {
					return row[0];
				}
			});
			
			$(".single_photo").colorbox();
            $(".related_iframe").colorbox({
                iframe: true,
                width: '800',
                height: '617'
            });
			
			$().bind('cbox_open', function() {
				embeds = document.getElementsByTagName('embed');
				for(i = 0; i < embeds.length; i++) {
					embeds[i].style.visibility = 'hidden';
				}
				
				objects = document.getElementsByTagName('object');
				for(i = 0; i < objects.length; i++) {
					objects[i].style.visibility = 'hidden';
				}
			});
			
			$().bind('cbox_closed', function() {
				embeds = document.getElementsByTagName('embed');
				for(i = 0; i < embeds.length; i++) {
					embeds[i].style.visibility = 'visible';
				}
				
				objects = document.getElementsByTagName('object');
				for(i = 0; i < objects.length; i++) {
					objects[i].style.visibility = 'visible';
				}
			});
			
			if (document.getElementById('showcase')) {
                iTV.startSlider();
            }
            else {
                iTV.stopSlider();
            }
		});
	},
    goto_anchor: function(id) {
        window.location = String(window.location).replace(/\#.*$/, "") + '#'+id;
    },
    toogler: function(element) {
        $(element).slideToggle("slow");
    },
	startSlider: function() {
	    $.doTimeout( 'slider', 10000, function(){
            iTV.slideSwitch();
            
            return true;
        });
    },
    stopSlider: function() {
        $.doTimeout( 'slider' );
    },
    setSlide: function(slide) {
        $.doTimeout( 'slider' );
        
        if (iTV.vars.on_slide) {
            $('li.'+iTV.vars.on_slide).find('.hover_img').css('opacity',1).removeClass('nohover');
            
            $('div.'+iTV.vars.on_slide).removeClass('active');
            $('div.'+iTV.vars.on_slide).css({opacity: 0});
            $('div.'+iTV.vars.on_slide).removeClass('active last-active');
            $('div.'+iTV.vars.on_slide).addClass('notactive');
        }
        
        $('li.'+slide).find('.hover_img').css('opacity',1).removeClass('nohover');
        $('li.'+slide).find('.hover_img').css('opacity',0).addClass('nohover');
        
        $('div.'+slide).css({opacity: 0.0})
            .removeClass('notactive')
            .addClass('active')
            .animate({opacity: 1.0}, 1000, function() {
                //$('div.'+iTV.vars.on_slide).removeClass('active last-active');
            });
            
        iTV.vars.on_slide = slide;
        
        iTV.startSlider();
    },
    slideSwitch: function() {
        var $active = $('#showcase DIV.active');
        
        if ( $active.length == 0 ) $active = $('#showcase DIV:last');
        
        var $next =  $active.next().length ? $active.next() : $('#showcase DIV:first');
        
        $active.addClass('notactive last-active');
        
        var prev_highlight_right_foto = $active.attr('class');
        var prev_split_highlight = prev_highlight_right_foto.split(' ');
        
        var promoted_class = prev_split_highlight[0];
        if (promoted_class == 'active') {
            promoted_class = prev_split_highlight[1];
        }
        $('li.'+promoted_class).find('.hover_img').css('opacity',1).removeClass('nohover');
        
        var highlight_right_foto = $next.attr('class');
        var split_highlight = highlight_right_foto.split(' ');
        
        var promoted_class_next = split_highlight[0];
        if (promoted_class_next == 'notactive') {
            promoted_class_next = split_highlight[1];
        }
        
        iTV.vars.on_slide = promoted_class_next;
        
        $next.css({opacity: 0.0})
            .removeClass('notactive')
            .addClass('active')
            .animate({opacity: 1.0}, 1000, function() {
                $active.removeClass('active last-active');
                $('li.'+promoted_class_next).find('.hover_img').css('opacity',0).addClass('nohover');
            });
        
        
    },
	ajaxPager: function(href, pager_tag, active, current) {
	    if (current) {
    		if($('#'+current).hasClass('act')) {
    			$("#"+current).removeClass('act');
    		}
        }
		
		if (active) {
			if (iTV.vars.current_active) {
				$("#"+iTV.vars.current_active).removeClass('act');
				iTV.vars.current_active = "";
			}
			
			$("#"+active).addClass('act');
			
			iTV.vars.current_active = active;
		}
		
		$("#"+pager_tag).ajaxStart(function(){
		      if ($.browser.msie) {
                    $("#"+pager_tag+" img").css('opacity', 0.3);
                    $("#"+pager_tag).css('opacity', 0.3);
              }
              else {
                    $("#"+pager_tag).css('opacity', 0.3);
              }
		});
		
		$.ajax({
			url: href,
			cache: false,
			success: function(html) {
			     if ($.browser.msie) {
                    $("#"+pager_tag+" img").css('opacity', 1);
                    $("#"+pager_tag).css('opacity', 1);
                 }
                 else {
                    $("#"+pager_tag).css('opacity', 1);
                 }
                
				$("#"+pager_tag).html(html);
			}
		});
		
		//$("#"+pager_tag).ajaxStop(function(){
		//	
		//});
        
        return false;
	},
	confirmDelete: function(href) {
		$("#dialogDeleteConfirm").dialog({
			bgiframe: true,
			resizable: false,
			draggable: false,
			height:160,
			width:350,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				'Usuń': function() {
					window.location=href;
					//$(this).dialog('close');
				},
				'Anuluj': function() {
					$(this).dialog('close');
				}
			}
		});
	},    
    dialogDisclaimer: function() {
		if (!$.cookie('disclaimer')) {
			embeds = document.getElementsByTagName('embed');
			for(i = 0; i < embeds.length; i++) {
				embeds[i].style.visibility = 'hidden';
			}
				
			objects = document.getElementsByTagName('object');
			for(i = 0; i < objects.length; i++) {
				objects[i].style.visibility = 'hidden';
			}

			iframes = document.getElementsByTagName('iframe');
			for(i = 0; i < iframes.length; i++) {
				iframes[i].style.visibility = 'hidden';
			}
			$("#dialogDisclaimer").dialog({
				bgiframe: true,
				resizable: false,
				draggable: false,
				height:600,
				width:600,
				closeText: 'hide',
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				buttons: {
					 'NIE AKCEPTUJĘ': function() {
						window.location.href = 'http://www.inwestortv.pl';
					},
					'AKCEPTUJĘ': function() {
						$.cookie('disclaimer', '1');
						embeds = document.getElementsByTagName('embed');
						for(i = 0; i < embeds.length; i++) {
							embeds[i].style.visibility = 'visible';
						}
						
						objects = document.getElementsByTagName('object');
						for(i = 0; i < objects.length; i++) {
							objects[i].style.visibility = 'visible';
						}

						iframes = document.getElementsByTagName('iframe');
						for(i = 0; i < iframes.length; i++) {
							iframes[i].style.visibility = 'visible';
						}
						$(this).dialog('close');
					}
				}
			});
		}
	}
}

function removeOptionSelected(select_id) {
	var elSel = document.getElementById(select_id);
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
		if (elSel.options[i].selected) {
			elSel.remove(i);
		}
	}
}

function deselectOptionAll(select_id) {
    var elSel = document.getElementById(select_id);
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
	   elSel.options[i].selected = false;
	}
}

function insertOption(prod_name, pid, select_id) {
	var elOptNew = document.createElement('option');
	elOptNew.text = prod_name;
	elOptNew.value = pid;
	
	var elSel = document.getElementById(select_id);
	breakAdding = false;
	
	for(i=0; i < elSel.options.length; i++) {
		if(elSel.options[i].value == pid){					  		
			breakAdding = true;
			break;
		}
	}			   	
	
	if (!breakAdding){					  
		try {
			elSel.add(elOptNew, null); 
		}
		catch(ex) {
			elSel.add(elOptNew); 
		}					  
	}
	else{
		alert("Ten element już istnieje na liscie");
		return false;
	}
}

function selectBeforeSubmit(select_id) {
	var productsV = "";
	var elSel = document.getElementById(select_id);
	for (i=0; i < elSel.options.length; i++ ){
		elSel.options[i].selected = "selected";
	}
}

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {
	if(ddmenuitem) {
		ddmenuitem.css('visibility', 'hidden');
	}
}

function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() { 
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = jsddm_close;
